I have a dialog that contains several checkboxes. When the dialog is created the boxes should be initialized as checked/unchecked according to the state of designated boolean variables. The variable check passes fine, but when I use CheckBox.setChecked(true/false) it crashes the app.
After a bit of research and playing around i've noticed that the Widget variable I'm using to point to the checkbox isn't getting initialized, though I have no idea why. I connect it with the corresponding id, but it doesn't seem to stick. Even stepping through the code, immediately after my findViewbyId() call the variable shows as null. I have no idea why.
(Although there are 5 checkboxes, I'm going to show the code for just one since they're all functionally identical)
MatrixActionActivity.java
public class MatrixActionActivity extends AppCompatActivity {
...
private CheckBox mHotSimCheck, mPublicCheck, mNoiseCheck, mDiffGridCheck, mSilentCheck;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_actions);
mHotSimCheck = (CheckBox)findViewById(R.id.checkbox_hotsim); // <-- mHotSimCheck registers as null even after this statement
//This button opens preferences dialog
mPrefsButt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MatrixActionActivity.this);
LayoutInflater inflater = MatrixActionActivity.this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_matrix_action_modifiers, null);
dialogBuilder.setView(dialogView);
final MyDeck deck = MyDeck.getInstance();
if(deck.isDiveDPActive) {
Toast.makeText(MatrixActionActivity.this, "deck.isDiveDPActive = true.", Toast.LENGTH_SHORT).show(); // <-- shows fine when next statement is removed
mHotSimCheck.setChecked(true); // <-- Causes crash
}else{
mHotSimCheck.setChecked(false);
}
dialogBuilder.setTitle("Dice Pool Modifier Preferences");
dialogBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//nada
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//nada
}
});
AlertDialog b = dialogBuilder.create();
b.show();
}
});
dialog_matrix_action_modifiers.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
...
<CheckBox
android:id="@+id/checkbox_hotsim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
android:text="+2 while in hot sim"
android:onClick="onCheckboxClicked"/>
</LinearLayout>
...
it's null
because the xml
in your setContentView
is activity_actions and the xml that contains checkbox is dialog_matrix_action_modifiers.xml , so obviously it will get null on your checkbox
after you build your dialog
mHotSimCheck = (CheckBox) dialogView.findViewById(R.id.checkbox_hotsim);
don't forget to remove this line after setcontentview
mHotSimCheck = (CheckBox)findViewById(R.id.checkbox_hotsim);
NB
where did you instantiate your variable mPrefsButt
?
The checkbox is not contained in the activity's xml, so that findViewById()
returns null.
You must initialize the CheckBox
inside onClick()
after you create dialogView
:
mHotSimCheck = (CheckBox) dialogView.findViewById(R.id.checkbox_hotsim);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.