Search code examples
androidpreferenceactivity

How to fix null pointer in PreferenceActivity?


In Android I suddenly get an error

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.preference.Preference.setOnPreferenceClickListener(android.preference.Preference$OnPreferenceClickListener)' on a null object reference
    at com.impyiablue.stoxx.UserSettingActivity.onCreate(UserSettingActivity.java:32)
    at android.app.Activity.performCreate(Activity.java:5977)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) 
    at android.app.ActivityThread.access$800(ActivityThread.java:148) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5272) 
    at java.lang.reflect.Method.invoke(Native Method) 
    ... 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) 

in this piece of code:

public class UserSettingActivity extends PreferenceActivity {

    private Preference myPreference;
    MainActivity.MyCallBack callBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // replaced on 5.1.2016
        // http://stackoverflow.com/questions/6822319/what-to-use-instead-of-addpreferencesfromresource-in-a-preferenceactivity
        //addPreferencesFromResource(R.xml.preferences);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();

        myPreference = findPreference("reset");
        myPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
             public boolean onPreferenceClick(Preference arg0) {
             ...

and the preferences.xml file looks as follows:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <EditTextPreference android:title="Your Name"
        android:key="username"
        android:summary="Please provide your username"></EditTextPreference>
    <CheckBoxPreference android:title="Application Updates"
        android:defaultValue="false"
        android:summary="This option if selected will allow the application to check for latest versions."
        android:key="applicationUpdates" />

    <Preference
        android:key="reset"
        android:title="Reset database"
        android:summary="This will remove every entry in the database"
         />
</PreferenceScreen>

It worked before (I cannot specify before what time). I just want to have an item 'reset' in my settings which will delete the database of the app. The method findPreference is striked through (implying it is some outdated code, without any clue how to 'fix' it).

How can I fix this null pointer exception?


Solution

  • After

    super.onCreate(savedInstanceState);
    

    add line

    addPreferencesFromResource(R.xml.preferences);
    

    addPreferencesFromResource needs to be called before you do anything with the preference.