我在活动中放置了一个复选框。
这是 XML:
<CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/preference_storingIDs" android:id="@+id/preferencecheckBoxSaveID" android:layout_below="@+id/preferenceImageButtonURL" android:layout_alignRight="@+id/preferenceImageButtonURL" android:layout_alignEnd="@+id/preferenceImageButtonURL" android:checked="true" />
在代码中:
初始化:
公共复选框 chkBoxSaveID;
并设置复选框:
chkBoxSaveID = (CheckBox) findViewById(R.id.preferencecheckBoxSaveID);
在其他活动中,同样的事情:
CheckBox chkBoxSaveID;
chkBoxSaveID = (CheckBox) findViewById(R.id.preferencecheckBoxSaveID);
然后,我创建了一个 if 条件。
if(chkBoxSaveID.isChecked()){
do blabla
}
else {
do blabla }
但是,当我运行模拟器时,我有这个错误:
引起:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“boolean android.widget.Checkbox.isChecked()”
我不明白为什么。我尝试将值固定为“True”,但它是相同的。你能给我解释一下吗?
问候,