我已经在stackoverflow上阅读了与此主题相关的各种线程,但似乎没有解决我遇到的这个特殊问题。
我使用以下对象作为接口:
public class FailGroupCreateEvent implements Serializable {
private OnEventListener mOnEventListener;
public void setOnEventListener(OnEventListener listener) {
mOnEventListener = listener;
}
public void doEvent() {
if (mOnEventListener != null) {
mOnEventListener.onEvent(); // event object :)
}
}
public interface OnEventListener extends Serializable {
void onEvent();
}
}
它就是这样,如果一个组无法创建Activity B
,它将通知Activity A
,Activity A
将加载一个快餐栏以显示失败消息。
在Activity A
中,我执行以下操作:
FailGroupCreateEvent failGroupCreateEvent = new FailGroupCreateEvent();
FailGroupCreateEvent.OnEventListener failedToCreateGroup = new FailGroupCreateEvent.OnEventListener() {
@Override
public void onEvent() {
Snackbar.make(getView(), getString(R.string.failed_group_create), Snackbar.LENGTH_LONG).show();
}
};
failGroupCreateEvent.setOnEventListener(failedToCreateGroup);
然后我使用以下代码启动Activity B
并将额外内容放入intent:
Intent intent = new Intent(context, ActivityB.class);
intent.putExtra(context.getString(R.string.failed_intent), failGroupCreateEvent);
startActivity(intent);
在ActivityB
中,我执行以下操作:
FailGroupCreateEvent failGroupCreateEvent;
if (//there is some type of network error) {
failGroupCreateEvent.doEvent();
}
但是,FailGroupCreateEvent对象抛出此错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp, PID: 4720
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.myapp.interfaces.FailGroupCreateEvent)
at android.os.Parcel.writeSerializable(Parcel.java:1394)
at android.os.Parcel.writeValue(Parcel.java:1341)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:644)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
at android.os.Bundle.writeToParcel(Bundle.java:1034)
at android.os.Parcel.writeBundle(Parcel.java:669)
at android.content.Intent.writeToParcel(Intent.java:7485)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2411)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1496)
at android.app.Activity.startActivityForResult(Activity.java:3745)
at android.app.Activity.startActivityForResult(Activity.java:3706)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:843)
at android.app.Activity.startActivity(Activity.java:4016)
at android.app.Activity.startActivity(Activity.java:3984)
at com.adapter.InnerPlaceAdapter$1.onClick(InnerPlaceAdapter.java:282)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.io.NotSerializableException: com.myapp.activities.PlacesFragment
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
at android.os.Parcel.writeSerializable(Parcel.java:1389)
at android.os.Parcel.writeValue(Parcel.java:1341)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:644)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
at android.os.Bundle.writeToParcel(Bundle.java:1034)
at android.os.Parcel.writeBundle(Parcel.java:669)
at android.content.Intent.writeToParcel(Intent.java:7485)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2411)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1496)
at android.app.Activity.startActivityForResult(Activity.java:3745)
at android.app.Activity.startActivityForResult(Activity.java:3706)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:843)
at android.app.Activity.startActivity(Activity.java:4016)
at android.app.Activity.startActivity(Activity.java:3984)
at com.adapter.InnerPlaceAdapter$1.onClick(InnerPlaceAdapter.java:282)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
为什么我不能将它作为两个活动之间的可序列化对象传递?
答案 0 :(得分:3)
从战术上讲,您试图通过Serializable
传递活动本身以及它所持有的所有内容,但getView()
提供了这些内容。
战略上,你想要的东西不起作用。 Serializable
用于将文件写入文件。文件无法保存活动,View
或事件侦听器。现在,Android恰好为Serializable
个额外内容提供了Intent
支持,但这并没有改变Serializable
的基本性质。如果无法将其写入文件,则无法正常工作。在最好的情况下,收件人最终会收到对象的副本,并且他们不会被绑定到发件人。
根据此处的情况,您需要:
使用startActivityForResult()
和onActivityResult()
或
使用活动总线,或
不要进行这两个单独的活动,而是将它们组合成一个活动,如果它们是紧密耦合的(例如,使用片段)