BadParcelableException - BroadcastReceiver意图崩溃

时间:2013-01-16 23:54:19

标签: android android-intent broadcastreceiver parcelable badparcelableexception

这个错误似乎偶尔会发生,这很奇怪。它通常很好,但偶尔会爬起来。这是我的代码,它位于BroadcastReceiver中:

public void onReceive(Context context, Intent intent) {
    MyCustomResponseMsg message = new MyCustomResponseMsg((MyMessage) intent.getParcelableExtra(MyMessage.EXTRA_MESSAGE_KEY));

    // do stuff with the message

    setResultCode(Activity.RESULT_OK);
}

以下是例外:

01-16 10:05:03.834: ERROR/AndroidRuntime(13533): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start receiver com.(path-to-broadcast-receiver).MyReqReceiver:   
android.os.BadParcelableException: ClassNotFoundException when unmarshalling:  com.(path-to-my-message).MyMessage
at android.app.ActivityThread.handleReceiver(ActivityThread.java:1805)
at android.app.ActivityThread.access$2400(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling:     com.(path-to-my-message).MyMessage
at android.os.Parcel.readParcelable(Parcel.java:1958)
at android.os.Parcel.readValue(Parcel.java:1846)
at android.os.Parcel.readMapInternal(Parcel.java:2083)
at android.os.Bundle.unparcel(Bundle.java:208)
at android.os.Bundle.getParcelable(Bundle.java:1100)
at android.content.Intent.getParcelableExtra(Intent.java:3437)
at     com.(path).MyReceiver.onReceive(My    ReqReceiver.java:23)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
... 10 more

错误似乎发生在getParcelableExtra行上。 “MyMessage”类(显然在我自己的项目中名称不同)确实存在;它包含在一个库中。它大部分时间都可以工作,但有时我每次收到广播时都会崩溃。

我意识到这一点并不多,但我只是想知道在接收器中是否有任何我可以采用不同的方式来捕获它以便它不会导致崩溃。或者如果它肯定是发送广播的一面,那么如果需要我可以专注于它。但在此期间,我只是想确保为我的目的正确设置。我已经检查过这次崩溃的其他类似帖子,但我似乎无法找到任何涉及从BroadcastReceiver中的意图获得的特定类型的parcelable。

提前感谢您对此有任何指导!

修改 如果需要,以下是MyMessage的构造函数:

public MyMessage(Parcel in) {
    field1 = in.readInt();
    field2 = in.readString();
    field3 = in.readString();
}

1 个答案:

答案 0 :(得分:8)

通过在解组自定义ClassLoader课程的过程中设置正确的Parcelable,我已经看到了此问题的大部分案例。

仔细查看您的MyMessage构造函数,特别是如何获取ClassLoader

private MyMessage(Parcel in) {
    //...
    //causes ClassNotFoundException
    this.field=in.readParcelable(null); 
    //causes ClassNotFoundException, in fact it's equivalent to previous
    this.field=in.readParcelable(ClassLoader.getSystemClassLoader()); 

    //this one should work
    this.field=in.readParcelable(getClass().getClassLoader());
}

可以找到更多详细信息in this question


更新:另外,请尝试为ClassLoader中包含的Bundle设置正确的Intent

public void onReceive(Context context, Intent intent) {
    Bundle extras=intent.getExtras();
    extras.setClassLoader(getClass().getClassLoader());
    MyCustomResponseMsg message = new MyCustomResponseMsg((MyMessage) extras.getParcelable(MyMessage.EXTRA_MESSAGE_KEY));
    //...
}