jeudi 4 novembre 2021

set up checkbox array in android

I have dialog which is supposed to have a set of checkboxes array that I am trying to implement. I have three checkboxes in the following code and it gives an error Attempt to invoke virtual method 'void android.widget.CheckBox.setChecked(boolean)' on a null object reference in this line checkboxes[i].setChecked(true);.

    case R.id.show_dialog:
            dialog = new Dialog(this);
            dialog.setContentView(R.layout.dialog);
            checkbox_layout = dialog.findViewById(R.id.checkbox_layout);

                CheckBox[] checkboxes = new CheckBox[3];
                for (int i=0;i < 3; i++) {
                    switch(i) {
                        case 0:
                            checkboxes[i] = dialog.findViewById(R.id.checkbox_1);
                            break;
                        case 1:
                            checkboxes[i] = dialog.findViewById(R.id.checkbox_2);
                            break;
                        case 2:
                            checkboxes[i] = dialog.findViewById(R.id.checkbox_3);
                            break;
                    }
               checkboxes[i].setChecked(true);
            



Aucun commentaire:

Enregistrer un commentaire