我刚刚了解了 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 有很大的问题。我用 View 绑定方法替换了上层类中的“findViewById”方法,我有以下代码:
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>