本文介绍了在Android中使用putSerializable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时putSerializable坏了?

当我传递null与putSerializable的FillSummary.class开始,因为它应该。如果我可是通过一个TestSerial对象比Android力关闭。

我有什么做的就是通过捆绑转移的自定义对象?

 公共类TestSerial实现Serializable {
    私有静态最后长的serialVersionUID = 2310640779687082782L;
    公众诠释我;
    公共TestSerial(){
        I = 1;
    }
}

私人无效renderSummary(){

    意向意图=新的意图(FillForm.this,FillSummary.class);
    捆绑额外=新包();

    extras.putSerializable(测试系列,新TestSerial());
    intent.putExtras(临时演员);

    startActivityForResult(意向,Forms.DISPLAY_SUMMARY);
}
 

错误:

  06-28 13:56:02.436:ERROR / AndroidRuntime(1136):未捕获的处理程序:螺纹主力退出,由于未捕获的异常
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):java.lang.RuntimeException的:不提供结果ResultInfo {谁= NULL,请求= 0,结果= -1,数据= {意向行动=返回用户的选择(有群众演员)}}到活动{com.Questionaire / com.Questionaire.FillForm}:java.lang.RuntimeException的:Parcelable遇到IOException异常写序列化对象(名称= com.Questionaire.FillForm $ TestSerial)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在android.app.ActivityThread.deliverResults(ActivityThread.java:3005)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在android.app.ActivityThread.handleSendResult(ActivityThread.java:3047)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在android.app.ActivityThread.access $ 2300(ActivityThread.java:112)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1721)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在android.os.Handler.dispatchMessage(Handler.java:99)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在android.os.Looper.loop(Looper.java:123)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在android.app.ActivityThread.main(ActivityThread.java:3948)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在java.lang.reflect.Method.invokeNative(本机方法)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在java.lang.reflect.Method.invoke(Method.java:521)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:782)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在dalvik.system.NativeStart.main(本机方法)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):java.lang.RuntimeException的:产生的原因Parcelable遇到IOException异常写序列化对象(名称= com.Questionaire.FillForm $ TestSerial)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在android.os.Parcel.writeSerializable(Parcel.java:1147)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在android.os.Parcel.writeValue(Parcel.java:1101)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在android.os.Parcel.writeMapInternal(Parcel.java:469)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在android.os.Parcel.writeBundle(Parcel.java:491)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在android.content.Intent.writeToParcel(Intent.java:4471)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1036)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在android.app.Instrumentation.execStartActivity(Instrumentation.java:1449)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在android.app.Activity.startActivityForResult(Activity.java:2656)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在com.Questionaire.FillForm.renderSummary(FillForm.java:206)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在com.Questionaire.FillForm.renderNext(FillForm.java:178)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在com.Questionaire.FillForm.onActivityResult(FillForm.java:86)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在android.app.Activity.dispatchActivityResult(Activity.java:3595)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在android.app.ActivityThread.deliverResults(ActivityThread.java:3001)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):11 ...更多
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):java.io.NotSerializableException:产生的原因com.Questionaire.FillForm
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1753)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:2073)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1900)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1863)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在java.io.ObjectOutputStream.writeFieldValues​​(ObjectOutputStream.java:1354)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:421)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1456)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1789)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:2073)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1900)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1863)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):在android.os.Parcel.writeSerializable(Parcel.java:1142)
06-28 13:56:02.784:ERROR / AndroidRuntime(1136):23 ...更多
 

解决方案

TestSerial 应该是静态的或在一个单独的文件中声明,这样就不会拖封闭非序列化类型。

Is putSerializable broken?

When I pass null with putSerializable the FillSummary.class starts as it should.If I however pass the a TestSerial object than Android force closes.

What do I have to do to get a custom object transfered via a 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);
}

Error:

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
解决方案

TestSerial should be either static or declared in a separate file so that it won't be dragging the enclosing non-serializable type.

这篇关于在Android中使用putSerializable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 16:15