mercredi 15 juillet 2015

checkbox.isChecked return "null" : Why ?! - Android

I have put a checkbox in a activity.

This is the 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" />

In the code :

Initializing :

public CheckBox chkBoxSaveID;

And set the checkbox:

chkBoxSaveID = (CheckBox) findViewById(R.id.preferencecheckBoxSaveID);

In other activity, same things:

 CheckBox chkBoxSaveID;
 chkBoxSaveID = (CheckBox) findViewById(R.id.preferencecheckBoxSaveID);

Then, i have create a if condition.

if(chkBoxSaveID.isChecked()){
do blabla
}
else {
do blabla }

but, when i running the emulator, i have this error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual methode 'boolean android.widget.Checkbox.isChecked()' on a null object reference

I don't understand why. I've try to fix the value at "True" but it's same. Can you explain me please ?

Regards,




Aucun commentaire:

Enregistrer un commentaire