0

ViewBinding について知りましたが、それを使用する必要があるかどうかがわかりません。基本的にはfindViewByIDを使っていて、かなり使いやすいと思います。ViewBinding について読みましたが、正直なところ、その使用方法がわかりませんでした。たとえば、次のコードがあります。

public class MainActivity extends AppCompatActivity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

        Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_mainActivity);
        setSupportActionBar(myToolbar);
        Button orderButton = findViewById(R.id.Bestellen_Button);
        orderButton.setOnClickListener(this);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        Button statisticsButton = findViewById(R.id.Statistik_Button);
        statisticsButton.setOnClickListener(this);

    }

ビューバインディングを使用するにはどうすればよいですか。Android 開発者ページ ( https://developer.android.com/topic/libraries/view-binding?utm_medium=studio-assistant-stable&utm_source=android-studio-3-6 )に記載されているように実行すると、エラーが発生します。

private ResultProfileBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ResultProfileBinding.inflate(getLayoutInflater());
    View view = binding.getRoot();
    setContentView(view);
}

たとえば、統計ボタンを参照するにはどうすればよいですか。

 Button statisticsButton = binding.findViewById(R.id.Statistik_Button);
            statisticsButton.setOnClickListener(this);

私はすべてのコメントに感謝し、あなたの助けに感謝します.

更新: ViewBinding にはまだ大きな問題があります。上位クラスの「findViewById」アプローチを View バインディング メソッドに置き換えました。次のコードがあります。

public class MainActivity extends AppCompatActivity implements OnClickListener {


    private ActivityMainBinding binding;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);


        /*

        Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_mainActivity);
        setSupportActionBar(myToolbar);
        Button orderButton = findViewById(R.id.Bestellen_Button);
        orderButton.setOnClickListener(this);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        Button statisticsButton = findViewById(R.id.Statistik_Button);
        statisticsButton.setOnClickListener(this);
         */
        Toolbar myToolbar = binding.toolbarMainActivity;
        setSupportActionBar(myToolbar);
        binding.BestellenButton.setOnClickListener(this);
        binding.StatistikButton.setOnClickListener(this);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

    }




    @Override
    public void onClick (View view){


        if(view.getId() == R.id.Bestellen_Button) {
            Intent intent_Selection = new Intent(this, Selection_Menu_Activity.class);
            startActivity(intent_Selection);


        }

        if(view.getId() == R.id.Statistik_Button) {
            Intent intent_Statistik = new Intent(this, CocktailY_Activity.class);
            startActivity(intent_Statistik);
        }
    }
}

ただし、アプリを実行すると、次のエラー メッセージが表示されます。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.td.barapp/com.example.td.barapp.MainActivity}: java.lang.NullPointerException: Attempt to read from field 'android.support.v7.widget.Toolbar com.example.td.barapp.databinding.ActivityMainBinding.toolbarMainActivity' on a null object reference

そして、「Toolbar myToolbar = binding.toolbarMainActivity;」という行 参照されます。このエラーが発生する理由について誰か意見がありますか? 私はすべての助けに感謝します。

クラスの対応する XML レイアウト ファイルは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"


    tools:context=".MainActivity"
    tools:ignore="ExtraText">




    '<!--Learning: The following lines define a toolbar -->'

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_mainActivity"
        android:layout_width="match_parent"
        android:layout_height="53dp"
        android:background="#435cb53f"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:titleTextColor="@android:color/holo_green_light" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_mainActivity_2"
        android:layout_width="match_parent"
        android:layout_height="53dp"
        android:background="#435cb53f"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:titleTextColor="@android:color/holo_green_light" />


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="427dp"
        android:layout_height="250dp"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent=".35"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.123"
        app:layout_constraintWidth_percent="1"

        app:srcCompat="@mipmap/vienna_test" />

    <Button
        android:id="@+id/Statistik_Button"
        android:layout_width="256dp"
        android:layout_height="95dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:background="#435cb53f"
        android:text="@string/Statistik_Button"
        android:textAllCaps="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.886" />

    <Button
        android:id="@+id/Bestellen_Button"
        android:layout_width="255dp"
        android:layout_height="96dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:background="#435cb53f"
        android:text="@string/Bestellen_Button"
        android:textAllCaps="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.614" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="153dp" />

    <TextView
        android:id="@+id/textView_ToolBar_MainActivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="App"
        android:textColor="@android:color/white"
        android:textSize="24sp"
        android:visibility="visible"
        app:fontFamily="@font/roboto_bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_bias="0.536"
        app:layout_constraintStart_toStartOf="@+id/toolbar_mainActivity"

        app:layout_constraintVertical_bias="0.0" />





</android.support.constraint.ConstraintLayout>
4

1 に答える 1