通过intent.putExtra传递接​​口时java.io.NotSerializableException

时间:2017-09-06 02:58:45

标签: android serialization interface deserialization

这是我从Serializable扩展的界面:

public interface InstaListener extends Serializable {
    void onSuccess(String response);
    void onFailure(int code, String reason);
}

以下是我在两个活动之间传递此监听器的方法:

InstaListener iListener = new InstaListener() {
            @Override
            public void onSuccess(String response) {

            }

            @Override
            public void onFailure(int code, String reason) {

            }
        };

Intent intent = new Intent(activity, Insta.class);
            intent.putExtra("email", email);
            intent.putExtra("phone", phone);
            intent.putExtra("purpose", purpose);
            intent.putExtra("amount", amountstr);
            intent.putExtra("name", name);
            intent.putExtra("env", env);
            intent.putExtra("listener", iListener);
            activity.startActivity(intent);

以下是我在Insta类中接收侦听器的方式。

instaListener = (InstaListener) getIntent().getSerializableExtra("listener");

我搜索了很多帖子,但没有解释为什么我收到错误: java.io.NotSerializableException

任何帮助?

修改 发布异常和堆栈跟踪

FATAL EXCEPTION: main
                                                                 Process: instamojo.testapp, PID: 6785
                                                                 java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = instamojo.library.InstamojoPay$1)
                                                                     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:3271)
                                                                     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1691)
                                                                     at android.app.Activity.startActivityForResult(Activity.java:4291)
                                                                     at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
                                                                     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
                                                                     at android.app.Activity.startActivityForResult(Activity.java:4249)
                                                                     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
                                                                     at android.app.Activity.startActivity(Activity.java:4603)
                                                                     at android.app.Activity.startActivity(Activity.java:4563)
                                                                     at instamojo.library.InstamojoPay.initInstamojo(InstamojoPay.java:61)
                                                                     at instamojo.library.InstamojoPay.start(InstamojoPay.java:34)
                                                                     at instamojo.testapp.MainActivity.callInstamojoPay(MainActivity.java:58)
                                                                     at instamojo.testapp.MainActivity.access$000(MainActivity.java:17)
                                                                     at instamojo.testapp.MainActivity$1.onClick(MainActivity.java:39)
                                                                     at android.view.View.performClick(View.java:5669)
                                                                     at android.view.View$PerformClick.run(View.java:22549)
                                                                     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:6290)
                                                                     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: instamojo.library.InstamojoPay
                                                                     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.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:3271) 
                                                                     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1691) 
                                                                     at android.app.Activity.startActivityForResult(Activity.java:4291) 
                                                                     at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50) 
                                                                     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79) 
                                                                     at android.app.Activity.startActivityForResult(Activity.java:4249) 
                                                                     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859) 
                                                                     at android.app.Activity.startActivity(Activity.java:4603) 
                                                                     at android.app.Activity.startActivity(Activity.java:4563) 
                                                                     at instamojo.library.InstamojoPay.initInstamojo(InstamojoPay.java:61) 
                                                                     at instamojo.library.InstamojoPay.start(InstamojoPay.java:34) 
                                                                     at instamojo.testapp.MainActivity.callInstamojoPay(MainActivity.java:58) 
                                                                     at instamojo.testapp.MainActivity.access$000(MainActivity.java:17) 
                                                                     at instamojo.testapp.MainActivity$1.onClick(MainActivity.java:39) 
                                                                     at android.view.View.performClick(View.java:5669) 
                                                                     at android.view.View$PerformClick.run(View.java:22549) 
                                                                     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:6290) 
                                                                     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) 

2 个答案:

答案 0 :(得分:1)

InstaListener的方法 - 本地实现是instamojo.library.InstamojoPay的内部类,它不是Serializable。您可能不希望它是Serializable,因此解决方案是将您的InstaListener实现编写为静态嵌套类,而不是方法本地内部类。

答案 1 :(得分:0)

你有一个内部类instamojo.library.InstamojoPay,它不可序列化。

要避免此异常,请将java.io.Serializable添加到内部类或将transient关键字添加到内部类对象声明