基本上我将对象保存为Bundle中的Serializable,然后通过putExtras()
将Bundle保存在Intent中。如果我在同一个Activity上从Bundle获得Serializable,我能够成功完成:
Bundle bundle = new Bundle();
bundle.putSerializable("value", myObj);
i.putExtras(bundle);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
MyObj temp = (MyObj) bundle.getSerializable("value");
}
}, 3000);
但是,当我调用startActivity()
移动到第二个Activity时,应用程序崩溃了:
02-28 10:07:29.576 14929-14929/com.mopub.simpleadsdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mopub.simpleadsdemo, PID: 14929
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.mopub.mobileads.MoPubView)
at android.os.Parcel.writeSerializable(Parcel.java:1527)
at android.os.Parcel.writeValue(Parcel.java:1475)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:724)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408)
at android.os.Bundle.writeToParcel(Bundle.java:1157)
at android.os.Parcel.writeBundle(Parcel.java:764)
at android.content.Intent.writeToParcel(Intent.java:8687)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3082)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
at android.app.Activity.startActivityForResult(Activity.java:4228)
at android.app.Activity.startActivityForResult(Activity.java:4186)
at android.app.Activity.startActivity(Activity.java:4525)
at android.app.Activity.startActivity(Activity.java:4493)
at com.mopub.simpleadsdemo.BannerActivity$2.run(BannerActivity.java:81)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.io.NotSerializableException: com.mopub.mobileads.AdViewController$1
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1224)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1584)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1549)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1472)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1218)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1584)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1549)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1472)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1218)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at android.os.Parcel.writeSerializable(Parcel.java:1522)
at android.os.Parcel.writeValue(Parcel.java:1475)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:724)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408)
at android.os.Bundle.writeToParcel(Bundle.java:1157)
at android.os.Parcel.writeBundle(Parcel.java:764)
at android.content.Intent.writeToParcel(Intent.java:8687)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3082)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
at android.app.Activity.startActivityForResult(Activity.java:4228)
at android.app.Activity.startActivityForResult(Activity.java:4186)
at android.app.Activity.startActivity(Activity.java:4525)
at android.app.Activity.startActivity(Activity.java:4493)
at com.mopub.simpleadsdemo.BannerActivity$2.run(BannerActivity.java:81)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
为什么会这样?我想在第二个Activity中检索该对象。我可以访问此第三方库(AdViewController
类)的源代码,并将其设为implements Serializable
。检查instanceof Serializable
表示myObj
已被序列化。
答案 0 :(得分:1)
问题的主要原因是:
Caused by: java.io.NotSerializableException: com.mopub.mobileads.AdViewController
表示您的类不可序列化。确保AdViewController还实现了可序列化的接口
答案 1 :(得分:0)
确保MyObj在您想要使用之前已实现Serializable接口 bundle.putSerializable(" value",myObj)。
{{1}}