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

我与影子孤独终老i 提交于 2019-12-12 23:54:03

问题


Folks, I have simplest class which I want to be Parcelable. I'm doing it in following way:

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];
        }
    };
}

Simple, heh? Yes, looks like. But when I try to send my class object to another activity as parcel object like:

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

I'm getting following exception:

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

Can't really understand what's here is unserializable?


回答1:


Everything is pretty simple: "CREATOR" in the Parcelable implementation should be the "public static final" field - not just "public".

By leaving the "CREATOR" field just "public" you're making it a member of the MyField class. And as "CREATOR" is not "Serializable" - so you're getting a crash while system attempts to serialize all members of the MyField class.




回答2:


I think the problem is you are mixing Serializable with Parcelable. No need to mix them both, Parcelable is Google's personal implementation of Serializable. Don't need to do it, it mixes thing up. For example, if you are creating a new MyField(), why are you casting in the next line of code. Try to clean up the code, focusing on "the Android way" to do it. Check the examples on the documentation.



来源:https://stackoverflow.com/questions/6035381/serializable-parcelable-issue-runtimeexception-parcelable-encountered-ioexcept

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!