19

Parcelable对于我的 Android 应用程序,我遇到了几个解组错误,尽管我认为我已经完成了通过s正确保存和加载对象所需的一切。你能告诉我我的代码有什么问题吗?

错误一:

java.lang.RuntimeException: Unable to start activity ComponentInfo
Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@41279860: Unmarshalling unknown type code 6619241 at offset 1372
at android.os.Parcel.readValue(Parcel.java:1922)
at android.os.Parcel.readMapInternal(Parcel.java:2094)
at android.os.Bundle.unparcel(Bundle.java:223)
at android.os.Bundle.getParcelable(Bundle.java:1158)
at android.app.Activity.onCreate(Activity.java:860)
at my.app.package.PlayComputer.onCreate(PlayComputer.java:1012)
at android.app.Activity.performCreate(Activity.java:4465)

第 1012 行 in是对in的MyActivity调用。super.onCreate(savedInstanceState);ActivityonCreate()

protected void onSaveInstanceState(Bundle savedInstanceState) {
    if (myObject == null) {
        savedInstanceState.putParcelable("myObject", null);
    }
    else {
        savedInstanceState.putParcelable("myObject", myObject);
    }
    savedInstanceState.putInt(...);
    savedInstanceState.putString(...);
    savedInstanceState.putBoolean(...);
    super.onSaveInstanceState(savedInstanceState);
}

myObject属于MyObject具有以下方法的类:

public void writeToParcel(Parcel out, int flags) {
    out.writeIntArray(...);
    out.writeInt(...);
    out.writeStringArray(...);
    out.writeString(...);
    out.writeParcelableArray(..., flags);
}

public static final Parcelable.Creator<MyObject> CREATOR = new Parcelable.Creator<MyObject>() {
    public MyObject createFromParcel(Parcel in) {
        try {
            if (in == null) {
                return null;
            }
            else {
                return new MyObject(in);
            }
        }
        catch (Exception e) {
            return null;
        }
    }
    public MyObject[] newArray(int size) {
        return new MyObject[size];
    }
};

private MyObject(Parcel in) {
    in.readIntArray(...);
    ... = in.readInt();
    in.readStringArray(...);
    ... = in.readString();
    ... = (OtherObject[]) in.readParcelableArray(OtherObject.class.getClassLoader());
}

错误2:

java.lang.RuntimeException: Unable to start activity ComponentInfo
Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling:
at android.os.Parcel.readParcelable(Parcel.java:1971)
at android.os.Parcel.readValue(Parcel.java:1859)
at android.os.Parcel.readMapInternal(Parcel.java:2099)
at android.os.Bundle.unparcel(Bundle.java:223)
at android.os.Bundle.getParcelable(Bundle.java:1158)
at android.app.Activity.onCreate(Activity.java:905)
at my.app.package.PlayComputer.onCreate(SourceFile:1012)

相同的文件和类。

错误 3:

java.lang.RuntimeException: Unable to start activity ComponentInfo
Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@4051aff8: Unmarshalling unknown type code 7340149 at offset 1276
at android.os.Parcel.readValue(Parcel.java:1913)
at android.os.Parcel.readMapInternal(Parcel.java:2083)
at android.os.Bundle.unparcel(Bundle.java:208)
at android.os.Bundle.getParcelable(Bundle.java:1100)
at my.app.package.PlayComputer.onCreate(SourceFile:1111)

这一次,导致行 (1111) 是以下行:

myObject = (MyObject) savedInstanceState.getParcelable("myObject");
4

3 回答 3

54

Android 有两种不同的类加载器:框架类加载器(知道如何加载 Android 类)和 APK 类加载器(知道如何加载代码)。APK 类加载器将框架类加载器设置为其父级,这意味着它也可以加载 Android 类。

错误 #2 可能是由使用框架类加载器的 Bundle 引起的,因此它不知道您的类。我认为当 Android 需要保留您的 Bundle 并稍后恢复它时(例如在后台内存不足时),可能会发生这种情况。您可以通过在包上设置 APK 类加载器来解决此问题:

savedInstanceState.setClassLoader(getClass().getClassLoader());

错误 #1 和 #3 更神秘,您是否可能在 中写入空值writeToParcel()?恐怕Android不太喜欢这样。

于 2012-12-30T19:12:12.710 回答
3

在从 bundle 读取之前,设置 ClassLoader:

bundle.setClassLoader(getClass().getClassLoader());

这节省了我的时间!

于 2016-11-19T17:18:00.043 回答
2

看起来,createFromParcelandnewArray应该像这样被覆盖:

public static final Parcelable.Creator<MyObject> CREATOR = new Parcelable.Creator<MyObject>() {
    @Override
    public MyObject createFromParcel(Parcel in) {
        MyObject myObj = new MyObject();
        myObj.intArray = in.readIntArray(...);
        myObj.intValue = in.readInt(...);
        // ....
        // IN THE SAME ORDER THAT IS WRITTEN OUT AS PER writeToParcel!
        //
        return myObj;
    }
    @Override
    public MyObject[] newArray(int size) {
        return new MyObject[size];
    }
};

编辑:

我忘了提到要使上述内容起作用,应该有一个空的构造函数!

public MyObject(){}
于 2012-12-21T21:54:48.393 回答