2017-09-06 90 views
0

這裏是我的界面,我從Serializable接口擴展:java.io.NotSerializableException而傳遞接口通過intent.putExtra

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)  
+0

在您的問題中發佈整個異常和堆棧跟蹤。 – EJP

+0

編輯&添加異常和堆棧跟蹤 – user1051505

回答

1

你的方法和本地的InstaListener實施是一個內部類的instamojo.library.InstamojoPay,這是不Serializable。您可能不希望它是可序列化的,因此解決方案是將您的InstaListener實現編寫爲靜態嵌套類,而不是方法本地內部類。

+0

這解決了它。問題:我無法從靜態類中訪問getApplicationContext()。我需要在回調中更新UI線程。 – user1051505

0

你有一個內部類的instamojo.library.InstamojoPay,它不是序列化。

爲了避免這種情況的例外,添加java.io.Serializable到您的內部類或添加transient關鍵字到您的內部類對象的聲明

+0

我做到了:public class InstamojoPay實現可序列化 - 它沒有幫助 – user1051505

+0

@ user1051505它確實有幫助。你沒有得到相同的錯誤信息。你遇到了一個不同的不可序列化的類。但是,使所有內容都可見Serializable不是正確的答案。 – EJP