在Android中使用putSerializable

时间:2010-06-28 13:42:40

标签: android

putSerializable是否已损坏?

当我使用putSerializable传递null时,FillSummary.class将按原样启动。 但是,如果我传递了一个TestSerial对象而不是Android强制关闭。

如何通过Bundle传输自定义对象需要做什么?

public class TestSerial implements Serializable{
    private static final long serialVersionUID = 2310640779687082782L;
    public int i;
    public TestSerial(){
        i=1;
    }
}

private void renderSummary(){

    Intent intent = new Intent(FillForm.this, FillSummary.class);
    Bundle extras = new Bundle();

    extras.putSerializable("test serial", new TestSerial());
    intent.putExtras(extras);

    startActivityForResult(intent, Forms.DISPLAY_SUMMARY);
}

错误:

06-28 13:56:02.436: ERROR/AndroidRuntime(1136): Uncaught handler: thread main exiting due to uncaught exception
06-28 13:56:02.784: ERROR/AndroidRuntime(1136): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { action=Return user choice (has extras) }} to activity {com.Questionaire/com.Questionaire.FillForm}: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.Questionaire.FillForm$TestSerial)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3005)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3047)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at android.app.ActivityThread.access$2300(ActivityThread.java:112)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1721)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at android.os.Looper.loop(Looper.java:123)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at android.app.ActivityThread.main(ActivityThread.java:3948)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at java.lang.reflect.Method.invokeNative(Native Method)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at java.lang.reflect.Method.invoke(Method.java:521)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at dalvik.system.NativeStart.main(Native Method)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136): Caused by: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.Questionaire.FillForm$TestSerial)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at android.os.Parcel.writeSerializable(Parcel.java:1147)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at android.os.Parcel.writeValue(Parcel.java:1101)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at android.os.Parcel.writeMapInternal(Parcel.java:469)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at android.os.Parcel.writeBundle(Parcel.java:491)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at android.content.Intent.writeToParcel(Intent.java:4471)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1036)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1449)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at android.app.Activity.startActivityForResult(Activity.java:2656)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at com.Questionaire.FillForm.renderSummary(FillForm.java:206)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at com.Questionaire.FillForm.renderNext(FillForm.java:178)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at com.Questionaire.FillForm.onActivityResult(FillForm.java:86)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at android.app.Activity.dispatchActivityResult(Activity.java:3595)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3001)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     ... 11 more
06-28 13:56:02.784: ERROR/AndroidRuntime(1136): Caused by: java.io.NotSerializableException: com.Questionaire.FillForm
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1753)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:2073)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1900)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1863)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1354)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:421)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1456)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1789)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:2073)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1900)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1863)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     at android.os.Parcel.writeSerializable(Parcel.java:1142)
06-28 13:56:02.784: ERROR/AndroidRuntime(1136):     ... 23 more

1 个答案:

答案 0 :(得分:11)

TestSerial应该是静态的,或者在单独的文件中声明,以便它不会拖动封闭的非可序列化类型。