-1

我有一个MainActivity包含TabHost其中一个选项卡的选项卡CardHolderActivity,它还包含一个TabHost带有两个选项卡的选项卡(它们Activities也是)。一切都很好,直到我设置了最新的之一 -MyCardsActivitySaveOnInstanceState将我的数据列表放在其中进行outState捆绑。我正在启动很多其他应用程序,所以我的应用程序被杀死了。在我从最近打开的应用程序列表中再次启动它之后,我得到了一个NullPointerException,这里是日志:

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)

这是我得到这个之后的代码:

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

cards是类型和类实现的static变量。TreeSet<Card>CardSerializable

你们能告诉我为什么会这样吗?另外,我很惊讶在将应用程序恢复为终止状态后发生这种情况,根本没有任何问题onSaveInstanceState()。而且我没有检索日期,也没有onRestoreInstanceState()检索onCreate()

4

1 回答 1

0

如果卡片是static,你为什么需要把它写进去onSavedInstanceState?作为静态的,它将绑定到 Class 对象,而不是 Activity 实例。

但是在活动中使用静态时要非常小心,因为这会导致内存泄漏。

于 2013-05-30T10:05:15.840 回答