简体   繁体   English

在onRestoreInstanceState中捆绑null?

[英]Bundle null in onRestoreInstanceState?

I recently received a crash in my program. 我最近在我的程序中遇到了崩溃。 Using logCat I determined that the crash was due to code in my onRestoreInstanceState() in the main activity when my app is being resumed. 使用logCat我确定崩溃是由于我的应用程序恢复时主活动中的onRestoreInstanceState()中的代码所致。 Logcat file: Logcat文件:

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

The crash occurs on the line mPanels = (Panel[]) inState.getParcelableArray("panelParcel"); 崩溃发生在mPanels = (Panel[]) inState.getParcelableArray("panelParcel"); in the onRestoreInstanceState method below. 在下面的onRestoreInstanceState方法中。

This methodology has worked robustly for me until this crash, and I'm trying to figure out why the inState appears to not have the array of objects (Panels) that I've saved in it. 这种方法对我来说非常有效,直到这次崩溃,我想弄清楚为什么inState似乎没有我保存在其中的对象数组(Panels)。 I'm confused because it seems like the inState doesnt include the panelParcel that I've saved to it. 我很困惑,因为看起来inState并没有包含我保存到它的panelParcel。

I see from this question I should typically be using onCreate instead of onRestoreInstanceState , but I'd love to understand why this wouldnt work in a rare case, perhaps I dont fully understand something about the Activity Lifecycle? 我从这个问题看,我通常应该使用onCreate而不是onRestoreInstanceState ,但是我很想知道为什么这种情况在极少数情况下不起作用,也许我对活动生命周期没有完全理解?

Code for both Save and Restore Instance State: 保存和还原实例状态的代码:

@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();
}

You cann't cast array of Parcelable to array of Panel . 你不能将Parcelable数组转换为Panel数组。

See casting Object array to Integer array error 请参阅将Object数组转换为Integer数组错误

Take a look at this post for more information onSaveInstanceState () and onRestoreInstanceState () 有关theSaveInstanceState()和onRestoreInstanceState()的更多信息,请查看这篇文章。

"onRestoreInstanceState() is called only when recreating activity after it was killed by the OS" “onRestoreInstanceState()仅在被OS杀死后重新创建活动时被调用”

and from the documentation: 并从文件:

Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. 大多数实现只是使用onCreate(Bundle)来恢复它们的状态,但有时在完成所有初始化之后在此处执行它或者允许子类决定是否使用默认实现。 The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle). 此方法的默认实现执行先前已被onSaveInstanceState(Bundle)冻结的任何视图状态的恢复。

Also this answer might be helpful https://stackoverflow.com/a/2909211/1241244 此答案可能会有所帮助https://stackoverflow.com/a/2909211/1241244

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android onRestoreInstanceState(Bundle savedInstanceState) - Android onRestoreInstanceState(Bundle savedInstanceState) 旋转后OnRestoreInstanceState返回null - OnRestoreInstanceState returning null after rotation 按钮的onRestoreInstanceState(Bundle)中的java.lang.NullPointerException - java.lang.NullPointerException in onRestoreInstanceState(Bundle) for Button onSaveInstanceState(Bundle) 和 onRestoreInstanceState(Bundle) 在哪个 scanario 中被触发? - In Which scanario's do onSaveInstanceState(Bundle) and onRestoreInstanceState(Bundle) get triggered? 替代onSaveInstanceState(Bundle)和onRestoreInstanceState(Bundle)的功能,用于在更改方向时保存数据 - Alternative to onSaveInstanceState(Bundle) and onRestoreInstanceState(Bundle), for save data when changing orientation 永不调用onRestoreInstanceState,/ saveInstanceState始终为null - onRestoreInstanceState is never called / savedInstanceState is always null 活动在onRestoreInstanceState()中失败-空指针异常 - Activity fails in onRestoreInstanceState() - Null Pointer Exception 如何从传递到 onCreate() 或 onRestoreInstanceState() 的已保存实例 state 包中受益于“热启动” - How to benefit on "warm start" from the saved instance state bundle passed into onCreate() or onRestoreInstanceState() Bundle bundle = new Bundle()处的Null Pointer异常; - Null Pointer exception at Bundle bundle=new Bundle(); 使用onRestoreInstanceState - Using of onRestoreInstanceState
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM