E/Parcel: Class not found when unmarshalling: com.xxx.xxx.XXXData Android无法接收带Parcelable的广播解决办法

Android无法接收带Parcelble的广播解决办法

示例代码

Intent intent = new Intent("xxx.xxx.ACTION");
Bundle bundle = new Bundle();
bundle.putParcelable("data", data);
 // 必须调用,否则可能会导致 android.os.BadParcelableException: ClassNotFoundException when unmarshalling:
bundle.setClassLoader(XXXData.class.getClassLoader());
intent.putExtra("ext", bundle);
//intent.setExtrasClassLoader(XXXData.class.getClassLoader());
// intent.putExtra(EXT_NAME_DATA, (Parcelable) data); // 会导致广播无法发送成功
getContext().sendBroadcast(intent);

错误日志

E/Parcel: Class not found when unmarshalling: com.xxx.xxx.XXXData
    java.lang.ClassNotFoundException: com.xxx.xxx.XXXData
        at java.lang.Class.classForName(Native Method)
        at java.lang.Class.forName(Class.java:453)
        at android.os.Parcel.readParcelableCreator(Parcel.java:2811)
        at android.os.Parcel.readParcelable(Parcel.java:2765)
        at android.os.Parcel.readValue(Parcel.java:2668)
        at android.os.Parcel.readArrayMapInternal(Parcel.java:3037)
        at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:288)
        at android.os.BaseBundle.unparcel(BaseBundle.java:232)
        at android.os.BaseBundle.getInt(BaseBundle.java:1030)
        at android.content.Intent.getIntExtra(Intent.java:7398)
        at android.app.LoadedApk$ReceiverDispatcher$InnerReceiver.performReceive(LoadedApk.java:1299)
        at android.app.ActivityThread$ApplicationThread.scheduleRegisteredReceiver(ActivityThread.java:1005)
        at android.app.IApplicationThread$Stub.onTransact(IApplicationThread.java:334)
        at android.os.Binder.execTransact(Binder.java:731)
     Caused by: java.lang.ClassNotFoundException: com.xxx.xxx.XXXData
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:1346)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:1406)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at java.lang.Class.classForName(Native Method) 
        at java.lang.Class.forName(Class.java:453) 
        at android.os.Parcel.readParcelableCreator(Parcel.java:2811) 
        at android.os.Parcel.readParcelable(Parcel.java:2765) 
        at android.os.Parcel.readValue(Parcel.java:2668) 
        at android.os.Parcel.readArrayMapInternal(Parcel.java:3037) 
        at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:288) 
        at android.os.BaseBundle.unparcel(BaseBundle.java:232) 
        at android.os.BaseBundle.getInt(BaseBundle.java:1030) 
        at android.content.Intent.getIntExtra(Intent.java:7398) 
        at android.app.LoadedApk$ReceiverDispatcher$InnerReceiver.performReceive(LoadedApk.java:1299) 
        at android.app.ActivityThread$ApplicationThread.scheduleRegisteredReceiver(ActivityThread.java:1005) 
        at android.app.IApplicationThread$Stub.onTransact(IApplicationThread.java:334) 
        at android.os.Binder.execTransact(Binder.java:731) 
     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

如果采用Bundle 方式发送广播,但未设置classloader,也会报错。

 Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.xxx.xxx.XXXData
    at android.os.Parcel.readParcelableCreator(Parcel.java:2839)
    at android.os.Parcel.readParcelable(Parcel.java:2765)
    at android.os.Parcel.readValue(Parcel.java:2668)
    at android.os.Parcel.readArrayMapInternal(Parcel.java:3037)
    at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:288)
    at android.os.BaseBundle.unparcel(BaseBundle.java:232)
    at android.os.Bundle.getParcelable(Bundle.java:940)
posted @ 2022-03-12 22:04  xiaomodecnblogs  阅读(346)  评论(0编辑  收藏  举报