gpt4 book ai didi

java - 获取 java.lang.RuntimeException : Unmarshalling unknown type code in Activity onRestoreInstanceState

转载 作者:行者123 更新时间:2023-11-29 05:38:21 26 4
gpt4 key购买 nike

我有一个 Activity,我在其中正确实现了 onSaveInstanceState()onRestoreInstanceState() 以维护 Activity状态。如果我旋转设备,一切都会成功,Activity 状态会保持不变。

当我将应用程序置于后台并使用 Task Killer 将其终止或在设备的开发人员选项中打开“不保留 Activity ”选项时,就会出现问题。在这种情况下,当我重新启动 Activity 时,出现以下异常

09-05 16:04:36.567: E/AndroidRuntime(1976): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vss.example/com.vss.example.ExampleActivity}: java.lang.RuntimeException: Parcel android.os.Parcel@b389dec8: Unmarshalling unknown type code 6619245 at offset 800
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.os.Looper.loop(Looper.java:137)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-05 16:04:36.567: E/AndroidRuntime(1976): at java.lang.reflect.Method.invokeNative(Native Method)
09-05 16:04:36.567: E/AndroidRuntime(1976): at java.lang.reflect.Method.invoke(Method.java:511)
09-05 16:04:36.567: E/AndroidRuntime(1976): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-05 16:04:36.567: E/AndroidRuntime(1976): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-05 16:04:36.567: E/AndroidRuntime(1976): at dalvik.system.NativeStart.main(Native Method)
09-05 16:04:36.567: E/AndroidRuntime(1976): Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@b389dec8: Unmarshalling unknown type code 6619245 at offset 800
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.os.Parcel.readValue(Parcel.java:2032)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.os.Parcel.readSparseArrayInternal(Parcel.java:2255)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.os.Parcel.readSparseArray(Parcel.java:1687)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.os.Parcel.readValue(Parcel.java:2022)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.os.Parcel.readMapInternal(Parcel.java:2226)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.os.Bundle.unparcel(Bundle.java:223)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.os.Bundle.getSparseParcelableArray(Bundle.java:1232)
09-05 16:04:36.567: E/AndroidRuntime(1976): at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1605)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.app.Activity.onRestoreInstanceState(Activity.java:928)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.app.Activity.performRestoreInstanceState(Activity.java:900)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1130)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2037)
09-05 16:04:36.567: E/AndroidRuntime(1976): ... 11 more

我知道当我们尝试以错误的顺序读取包裹时会发生这种情况,但如果是这样的话,当我们旋转时不应该发生Exception设备也是?我相信它遵循完全相同的过程,但在今天之后我不能 100% 确定。

顺便说一下,这只发生在我添加了自定义 ViewActivity 中。实现与支持库中的 FragmentTabHost 相同(实际上,我复制并修改了该源代码),除了我所做的一些更改。但是更改不会影响以前的 FragmentTabHost 状态代码。以下是相关的代码 fragment -

static class SavedState extends BaseSavedState {

String curTab;

SavedState(final Parcelable superState) {

super(superState);
}

private SavedState(final Parcel in) {

super(in);
curTab = in.readString();
}

@Override
public void writeToParcel(final Parcel out, final int flags) {

super.writeToParcel(out, flags);
out.writeString(curTab);
}

@Override
public String toString() {

return "SavedStateFragmentTabHost.SavedState{"
+ Integer.toHexString(System.identityHashCode(this))
+ " curTab=" + curTab + "}";
}

public static final Parcelable.Creator<SavedState> SAVED_STATE_CREATOR = new Parcelable.Creator<SavedState>() {

@Override
public SavedState createFromParcel(final Parcel in) {

return new SavedState(
in);
}

@Override
public SavedState[] newArray(final int size) {

return new SavedState[size];
}
};
}

@Override
protected Parcelable onSaveInstanceState() {

final Parcelable superState = super.onSaveInstanceState();
final SavedState ss = new SavedState(superState);
ss.curTab = getCurrentTabTag();
return ss;
}

@Override
protected void onRestoreInstanceState(final Parcelable state) {

final SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
setCurrentTabByTag(ss.curTab);
}

如果我在自定义 FragmentTabHost 中注释掉 onSaveInstanceState()onRestoreInstanceState() 方法,Exception 不会发生。以前有其他人遇到过这个问题吗?你能设法修复它吗?

最佳答案

我怀疑您必须将 SAVED_STATE_CREATOR 重命名为 CREATOR 才能使其正常工作。此静态字段的名称是 Parcelable 的一部分的契约(Contract)。

关于java - 获取 java.lang.RuntimeException : Unmarshalling unknown type code in Activity onRestoreInstanceState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18635598/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com