Serialized Parcelable 问题:RuntimeException: Parcelable 在写入可序列化对象时遇到 IOException
各位,我有一个最简单的类,我想将其打包。我按照以下方式做:
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];
}
};
}
很简单,呵呵?是的,看起来像。但是,当我尝试将我的类对象作为包裹对象发送到另一个活动时,例如:
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
无法真正理解这里的内容是不可序列化的?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一切都非常简单:Parcelable 实现中的“CREATOR”应该是“public static final”字段 - 而不仅仅是“public”。
通过将“CREATOR”字段保留为“public”,您将使其成为 MyField 类的成员。由于“CREATOR”不是“可序列化的”,因此当系统尝试序列化 MyField 类的所有成员时,您会遇到崩溃。
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.
我认为问题在于您将
Serialized
与Parcelable
混合在一起。无需将两者混合使用,Parcelable
是 Google 个人对Serialized
的实现。不需要这样做,它会把事情搞混。例如,如果您要创建一个新的MyField()
,为什么要转换下一行代码。尝试清理代码,重点关注“Android 方式”。查看文档上的示例。I think the problem is you are mixing
Serializable
withParcelable
. No need to mix them both,Parcelable
is Google's personal implementation ofSerializable
. Don't need to do it, it mixes thing up. For example, if you are creating a newMyField()
, 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.