-1

I have a MainActivity which holds TabHost one of its tabs is CardHolderActivity which also contains a TabHost with two tabs (and they are Activities also). Everything goes fine until after I set in one of the latest - MyCardsActivity - the SaveOnInstanceState where I put a list of my data to outState bundle. I'm starting a lot of other apps so my app is being killed. After I start it again from the list of recently opened application I'm getting a NullPointerException, here is the log:

05-30 12:35:06.032: I/CrashLogger(28147): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.package/my.package.activities.MainActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{my.package/my.package.activities.CardsHolderActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{my.package/my.package.activities.MyCardsActivity}: java.lang.NullPointerException
05-30 12:35:06.032: I/CrashLogger(28147): --------- Stack trace ---------
05-30 12:35:06.032: I/CrashLogger(28147):     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-30 12:35:06.032: I/CrashLogger(28147):     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-30 12:35:06.032: I/CrashLogger(28147):     android.app.ActivityThread.access$600(ActivityThread.java:141)
05-30 12:35:06.032: I/CrashLogger(28147):     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-30 12:35:06.032: I/CrashLogger(28147):     android.os.Handler.dispatchMessage(Handler.java:99)
05-30 12:35:06.032: I/CrashLogger(28147):     android.os.Looper.loop(Looper.java:137)
05-30 12:35:06.032: I/CrashLogger(28147):     android.app.ActivityThread.main(ActivityThread.java:5041)
05-30 12:35:06.032: I/CrashLogger(28147):     java.lang.reflect.Method.invokeNative(Native Method)
05-30 12:35:06.032: I/CrashLogger(28147):     java.lang.reflect.Method.invoke(Method.java:511)
05-30 12:35:06.032: I/CrashLogger(28147):     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-30 12:35:06.032: I/CrashLogger(28147):     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-30 12:35:06.032: I/CrashLogger(28147):     dalvik.system.NativeStart.main(Native Method)

Here is the code after which I have this:

@Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (cards != null)
            outState.putSerializable(EXTRA_MY_CARDS, cards);
    }

The cards is a static variable of TreeSet<Card> type and Card class implements Serializable.

Can you guys tell me why this happens? Also, I'm very surprised this happens after restoring the application for killed state, there is no any issues on the onSaveInstanceState() at all. And I'm not retrieving the date nor in onRestoreInstanceState() and onCreate() neither.

Eugene
  • 59,186
  • 91
  • 226
  • 333

1 Answers1

0

If cards is static, why do you need to write it in onSavedInstanceState? Being static, it will be tied to Class object, not to Activity instance.

But be very careful when using static within Activities as that leads to memory leaks.

gunar
  • 14,660
  • 7
  • 56
  • 87
  • Because if the application is killed you lose the data from the static references. – Eugene May 30 '13 at 10:06
  • 1
    "if the application is killed" then you will lose everything from your app, no matter what it holds. onSavedInstanceState referers to cases when the ACTIVITY is killed, not the application. Taken from onSavedInstanceState documentation: "Called to retrieve per-instance state from an activity before being killed so that the state can be restored" – gunar May 30 '13 at 10:09
  • AFAIK the Activity can't be killed alone, the whole Application is killed if OS requires resources. – Eugene May 30 '13 at 10:10
  • 1
    I would say you are incorrect ... please have a look into Activity life-cycle: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle – gunar May 30 '13 at 10:12
  • Please check what Dianne Hackborn is saying about this http://stackoverflow.com/questions/7536988/android-app-out-of-memory-issues-tried-everything-and-still-at-a-loss/7576275#7576275 . `And when it does kill its process, it will kill the process hosting all the activities, including whatever is currently in the foreground.` – Eugene May 30 '13 at 10:20
  • 1
    OK, that's true for the obvious reason as all application activities run in the application process. But, reading more carefully what Dianne wrote, that doesn't mean the activity is killed ONLY when the process is killed :) – gunar May 30 '13 at 10:26
  • Thanks for comments :) Anyway this doesn't resolve my NullPointerException what the question was about ;) – Eugene May 30 '13 at 10:30
  • 1
    Why don't you remove the onSavedInstanceState method? That should fix the NPE – gunar May 30 '13 at 10:32
  • Because I need to persist the data received from the server and do not retrieve it each time after Activity is killed. That's what onSaveInstanceState is for. – Eugene May 30 '13 at 10:36