mardi 14 mai 2019

CheckBox.setChecked() causes crash; CheckBox variable not initializing, always null?

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);

//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();
                    mHotSimCheck.setChecked(true);
                }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>
...




Aucun commentaire:

Enregistrer un commentaire