Search code examples
androidsharedpreferencesonresume

Unable to resume activity - SharedPreferences


I'm trying to implement a "first-launch" method that will open a specific activity the first time the user opens the app.

I have a SharedPreferences called prefs:

public class MainActivity extends ActionBarActivity {

SharedPreferences prefs = null;

public static final String DEFAULT = "N/A";

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

Now, in my onResume method:

    @Override
    protected void onResume() {
    super.onResume();

    if (prefs.getBoolean("firstrun", true)) {
        // Do first run stuff here then set 'firstrun' as false
        // using the following line to edit/commit prefs


        Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show();


        prefs.edit().putBoolean("firstrun", false).apply();
        }
    }

This works totally fine in other apps where I want to have this feature as well. But in this project, it fails:

    java.lang.RuntimeException: Unable to resume activity {com.heavyfork.partystarter/com.heavyfork.partystarter.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.content.SharedPreferences.getBoolean(java.lang.String, boolean)' on a null object reference
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3224)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3257)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2479)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5696)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
            Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.content.SharedPreferences.getBoolean(java.lang.String, boolean)' on a null object reference
            at com.heavyfork.partystarter.MainActivity.onResume(MainActivity.java:258)
            at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1331)
            at android.app.Activity.performResume(Activity.java:6044)

            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3207)
            
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3257)
            
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2479)        
    at android.app.ActivityThread.access$800(ActivityThread.java:144)       
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)      
at android.os.Looper.loop(Looper.java:155)           
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
            
at java.lang.reflect.Method.invoke(Method.java:372)
            
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
            
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

Sorry for the weird code format.

Does anyone know what might cause this, I think it had something to do with the onResume method?


Solution

  • You have initialized your SharedPreferences prefs as null, hence the NPE.

    To access the default SharedPreferences, do like below

    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    boolean first = prefs.getBoolean("firstrun", true)
    

    then, you can go ahead and do

    if (first) {
        // Do first run stuff here then set 'firstrun' as false
        // using the following line to edit/commit prefs
    
    
        Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show();
    
    
        prefs.edit().putBoolean("firstrun", false).apply();
        }
    }
    

    in your onCreate method