-1

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,

Rémy BRILLET
  • 295
  • 1
  • 4
  • 8
  • **WILD GUESS**: You didn't set the **ContentView** to the same layout which contains that CheckBox. Therefore, it can't find that CheckBox, and returns **null**. – Phantômaxx Jul 15 '15 at 17:55
  • Hi Der Gol...lum, thanks for your help. If I understand, before i use the checkbox, i need to modify the ContentView like that : > setContentView(R.layout.activity_preferences_accueil); The checkbox is on the layout preferences, and i need to check the state of the checkbox in the login layout. – Rémy BRILLET Jul 16 '15 at 08:04
  • Yes. If you don't have the View in the current layout, that View won't be found by `findViewById()`. – Phantômaxx Jul 16 '15 at 08:07
  • so I can write something like this ? setContentView(R.layout.activity_preferences_accueil); chkBoxSaveID = (CheckBox) findViewById(R.id.preferencecheckBoxSaveID); // Test with my checkbox setContentView(R.layout.Activiy_login); – Rémy BRILLET Jul 16 '15 at 08:22
  • Yes, but... WHY? you can check a preference WITHOUT the PreferenceScreen. – Phantômaxx Jul 16 '15 at 08:26
  • I've understand that when you have said " If you don't have the View in the current layout, that View won't be found by findViewById()" Sorry i'm a newbee in Android.. Can you explain differently ? Please – Rémy BRILLET Jul 16 '15 at 09:18
  • If you are only going to retrieve a preference value, you can do that **directly**. You **don't need** to set the layout to that of the PreferenceScreen, to retrieve a preference value. **No Views** involved. Just **get the values** from the preference file. As explained here: http://developer.android.com/guide/topics/ui/settings.html#ReadingPrefs – Phantômaxx Jul 16 '15 at 09:39
  • From the preference file ? How ? – Rémy BRILLET Jul 16 '15 at 09:44
  • I updated my comment. Follow that link. – Phantômaxx Jul 16 '15 at 09:45
  • 1
    Thank you, I will check that. – Rémy BRILLET Jul 16 '15 at 09:47

1 Answers1

0

The error says that your CheckBox view is null.

Make sure that the following line returns a valid CheckBox object and not null:

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

MarkySmarky
  • 1,609
  • 14
  • 17