1

我最近在我的程序中收到了崩溃。使用 logCat,我确定崩溃是由于onRestoreInstanceState()我的应用程序恢复时主要活动中的代码造成的。日志文件:

07-23 16:27:01.927 I/ActivityManager(  390): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.myapp/.MainActivity bnds=[1184,809][1376,1001]} from pid 30666

07-23 16:27:01.997 I/ActivityManager(  390): Start proc com.myapp for activity com.myapp/.MainActivity: pid=31007 uid=10070 gids={50070, 1028}

07-23 16:27:02.137 E/AndroidRuntime(31007): FATAL EXCEPTION: main


07-23 16:27:02.137 E/AndroidRuntime(31007): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.MainActivity}: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.myapp.Panel[]

07-23 16:27:02.137 E/AndroidRuntime(31007):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)

07-23 16:27:02.137 E/AndroidRuntime(31007):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

07-23 16:27:02.137 E/AndroidRuntime(31007):     at android.app.ActivityThread.access$600(ActivityThread.java:141)

07-23 16:27:02.137 E/AndroidRuntime(31007):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)

07-23 16:27:02.137 E/AndroidRuntime(31007):     at android.os.Handler.dispatchMessage(Handler.java:99)

07-23 16:27:02.137 E/AndroidRuntime(31007):     at android.os.Looper.loop(Looper.java:137)

07-23 16:27:02.137 E/AndroidRuntime(31007):     at android.app.ActivityThread.main(ActivityThread.java:5041)

07-23 16:27:02.137 E/AndroidRuntime(31007):     at java.lang.reflect.Method.invokeNative(Native Method)

07-23 16:27:02.137 E/AndroidRuntime(31007):     at java.lang.reflect.Method.invoke(Method.java:511)

07-23 16:27:02.137 E/AndroidRuntime(31007):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

07-23 16:27:02.137 E/AndroidRuntime(31007):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

07-23 16:27:02.137 E/AndroidRuntime(31007):     at dalvik.system.NativeStart.main(Native Method)

07-23 16:27:02.137 E/AndroidRuntime(31007): Caused by: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.myapp.Panel[]

07-23 16:27:02.137 E/AndroidRuntime(31007):     at com.myapp.MainActivity.onRestoreInstanceState(MainActivity.java:177)

07-23 16:27:02.137 E/AndroidRuntime(31007):     at android.app.Activity.performRestoreInstanceState(Activity.java:910)

07-23 16:27:02.137 E/AndroidRuntime(31007):     at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1131)

07-23 16:27:02.137 E/AndroidRuntime(31007):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2158)

07-23 16:27:02.137 E/AndroidRuntime(31007):     ... 11 more

07-23 16:27:02.167 W/ActivityManager(  390):   Force finishing activity com.myapp/.MainActivity

崩溃发生在mPanels = (Panel[]) inState.getParcelableArray("panelParcel");下面的 onRestoreInstanceState 方法中。

这种方法对我来说一直有效,直到这次崩溃,我试图弄清楚为什么 inState 似乎没有我保存在其中的对象数组(面板)。我很困惑,因为 inState 似乎不包括我保存到其中的 panelParcel。

我从这个问题中看到我通常应该使用onCreate而不是onRestoreInstanceState,但我很想了解为什么这在极少数情况下不起作用,也许我不完全了解活动生命周期的某些内容?

保存和恢复实例状态的代码:

@Override
public void onSaveInstanceState(@NotNull Bundle outState)
{
    Log.d("panelCreation", "onSaveInstanceState Called");
    super.onSaveInstanceState(outState);
    // save the current panel state
    outState.putParcelableArray("panelParcel", mPanels);
}

@Override
public void onRestoreInstanceState(Bundle inState)
{
    Log.d("panelCreation", "onsState Called");
    super.onRestoreInstanceState(inState);

    // Note getParcelable returns a *new* array, so we must setup the drawer listener after this
    mPanels = (Panel[]) inState.getParcelableArray("panelParcel");
    // set up the drawer's list view with items and click listener
    mDrawerList.setAdapter(new PanelArrayAdapter(this,
            R.layout.drawer_list_item, mPanels));
    refreshDrawerListChecked();

    // Forces the Adapter to redraw the view to ensure color stylings are applied
    ((PanelArrayAdapter)mDrawerList.getAdapter()).notifyDataSetChanged();
}
4

2 回答 2

3

您不能将数组Parcelable转换为数组Panel

请参阅将对象数组转换为整数数组错误

于 2013-07-24T15:16:37.420 回答
0

看看这篇文章了解更多关于SaveInstanceState()和onRestoreInstanceState()的信息

“onRestoreInstanceState() 仅在被操作系统杀死后重新创建活动时调用”

并从文档中:

大多数实现将简单地使用 onCreate(Bundle) 来恢复它们的状态,但有时在完成所有初始化之后在这里这样做会很方便,或者允许子类决定是否使用您的默认实现。该方法的默认实现会恢复之前被 onSaveInstanceState(Bundle) 冻结的任何视图状态。

这个答案也可能有帮助https://stackoverflow.com/a/2909211/1241244

于 2013-07-24T15:10:13.533 回答