简体   繁体   中英

Getting crash in Android on onResume() in my activity - Why?

The error is:

Unable to resume activity: java.lang.NullPointerException

Here is the stack trace:

0 java.lang.RuntimeException: Unable to resume activity {com.sortitapps.movies/com.sortitapps.movies.SettingsActivity}: java.lang.NullPointerException
1 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2944)
2 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2973)
3 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2408)
4 at android.app.ActivityThread.access$600(ActivityThread.java:165)
5 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
6 at android.os.Handler.dispatchMessage(Handler.java:107)
7 at android.os.Looper.loop(Looper.java:194)
8 at android.app.ActivityThread.main(ActivityThread.java:5391)
9 at java.lang.reflect.Method.invokeNative(Native Method)
10 at java.lang.reflect.Method.invoke(Method.java:525)
11 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
13 at dalvik.system.NativeStart.main(Native Method)
14 Caused by: java.lang.NullPointerException
15 at com.sortitapps.movies.SettingsActivity.onResume(SettingsActivity.java:149)
16 at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1259)
17 at android.app.Activity.performResume(Activity.java:5200)
18 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2931)

And here is the code. Line 149 is adapter.notifyDataSetChanged(); :

@Override
public void onResume() 
{
    super.onResume();  // Always call the superclass method first

    SharedPreferences settings = getSharedPreferences("prefs", 0);

    int numberDesktop = settings.getInt("desktop", 0);
    Log.i("Desktop purchased:", String.valueOf(numberDesktop));

    int numberScans = settings.getInt("scanning", 0);
    Log.i("Number of scans:", String.valueOf(numberScans));

    if (settingsitems.contains("Unlock Desktop Apps") &&
            numberDesktop == 2)
    {
        settingsitems.remove("Unlock Desktop Apps");
    }

    String freeapp = getResources().getString(R.string.free_app);

    if (settingsitems.contains("Unlock Premium Features") &&
            freeapp.equals("Yes") && 
            numberScans > 10)
    {
        settingsitems.remove("Unlock Premium Features");
    }

    adapter.notifyDataSetChanged();
}

Do I need to initialize the adapter again when coming resuming?

Most certainly adapter is null.

Your code doesn't show how the adapter got initialized, so it could be that it wasn't initialized with a value, or it was set to null after that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM