Serializable parcelable issue:RuntimeException:Parcelable遇到IOException,编写可序列化对象

时间:2011-05-17 18:24:59

标签: android serialization serializable parcelable

伙计们,我有最简单的课,我想成为Parcelable。我是按照以下方式做的:

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];
        }
    };
}
简单,嘿?是的,看起来像。但是当我尝试将我的类对象作为parcel对象发送到另一个活动时,如:

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

我遇到了以下异常:

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

无法真正理解这里的不可序列化的内容?

2 个答案:

答案 0 :(得分:6)

一切都很简单:Parcelable实现中的“CREATOR”应该是“公共静态最终”字段 - 而不仅仅是“公共”。

通过将“CREATOR”字段保留为“public”,您将使其成为MyField类的成员。并且因为“CREATOR”不是“Serializable” - 所以当系统尝试序列化MyField类的所有成员时,你会遇到崩溃。

答案 1 :(得分:2)

我认为问题在于您将SerializableParcelable混合在一起。无需将它们混合使用,Parcelable是Google对Serializable的个人实施。不需要这样做,它混合起来。例如,如果您要创建新的MyField(),为什么要在下一行代码中进行投射。尝试清理代码,专注于“Android方式”来做到这一点。查看documentation上的示例。