关于android:Serializable parcelable问题:RuntimeException:Parcelable遇到IOException写入可序列化对象

Serializable parcelable issue: RuntimeException: Parcelable encountered IOException writing serializable object

伙计们,我有最简单的类,我想成为Parcelable。我是这样做的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class MyField implements Serializable, Parcelable
{

    public MyField()
    {
    }


    //************************************************
    // Parcelable methods
    //************************************************
    public int describeContents() {return 0; };

    public void writeToParcel(Parcel out, int flags)
    {
        out.writeInt(this.describeContents());
        out.writeSerializable(this);
    }

    @SuppressWarnings("redundant")      
    public Parcelable.Creator<MyField> CREATOR
            = new Parcelable.Creator<MyField>()
    {
        public MyField createFromParcel(Parcel in)
        {
            int description=in.readInt();
            Serializable s=in.readSerializable();
            switch(description)
            {
                case 0:
                    return (MyField )s;
                default:
                    return null;
            }
        }

        public MyField[] newArray(int size)
        {
            return new MyField[size];
        }
    };
}

简单,哈?是的,看起来像。但当我尝试将类对象作为包对象发送到另一个活动时,例如:

1
2
3
4
    intent=new Intent(activity, SendMessageActivity.class);
    MyField fld=new MyField();
    intent.putExtra("to", (Parcelable )fld);
    activity.startActivity(intent);

我有以下例外:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = ru.ivanovpv.android.storage.MyField)
at android.os.Parcel.writeSerializable(Parcel.java:1131)
at ru.ivanovpv.android.storage.MyField.writeToParcel(MyField.java:58)
at android.os.Parcel.writeParcelable(Parcel.java:1106)
at android.os.Parcel.writeValue(Parcel.java:1029)
at android.os.Parcel.writeMapInternal(Parcel.java:469)
at android.os.Bundle.writeToParcel(Bundle.java:1445)
at android.os.Parcel.writeBundle(Parcel.java:483)
at android.content.Intent.writeToParcel(Intent.java:5237)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1204)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1373)
at android.app.Activity.startActivityForResult(Activity.java:2749)
at android.app.Activity.startActivity(Activity.java:2855)
at ru.ivanovpv.android.storage.BulkContact.doContextItemSelected(BulkContact.java:105)
at ru.ivanovpv.android.cellmessenger.ContactsActivity.onContextItemSelected(ContactsActivity.java:188)
at android.app.Activity.onMenuItemSelected(Activity.java:2174)
at com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback.onMenuItemSelected(PhoneWindow.java:2731)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:129)
at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:884)
at android.widget.AdapterView.performItemClick(AdapterView.java:284)
at android.widget.ListView.performItemClick(ListView.java:3285)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.io.NotSerializableException: ru.ivanovpv.android.storage.MyField$1
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1547)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1854)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1696)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1660)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1854)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1696)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1660)
at android.os.Parcel.writeSerializable(Parcel.java:1126)
... 31 more

真的不明白这里有什么是不可销售的?


一切都很简单:parcelable实现中的"creator"应该是"public static final"字段,而不仅仅是"public"。

通过只将"创建者"字段"公开",您可以使它成为MyField类的成员。因为"创建者"不是"可序列化的"——所以当系统试图序列化MyField类的所有成员时,您会遇到崩溃。


我想问题是你把SerializableParcelable混在一起了。不需要两者混合,Parcelable是谷歌对Serializable的个人实现。不需要这样做,它把事情搞混了。例如,如果您要创建一个新的MyField(),为什么要在下一行代码中强制转换。试着清理代码,专注于"Android方式"。检查文档中的示例。