Android中跨进程通信传递Parcelable对象时出现android.os.BadParcelableException: ClassNotFoundException when unmarsh
来源:互联网 发布:安知玉如意说的是什么 编辑:程序博客网 时间:2024/06/01 09:25
转载请注明出处:http://blog.csdn.net/bettarwang/article/details/45315091
按Google开发文档的说法,在跨进程通信时,推荐使用MessengerService而不是AIDL,所以最近在实现一个跨进程的Service时就采用了MessengerService的方法。
然后定义了这样一个类:
public class BleServiceBean implements Parcelable { private String name; private String uuid; public BleServiceBean() { } public BleServiceBean(BluetoothGattService service) { uuid=service.getUuid().toString(); name= BleNamesResolver.resolveServiceName(uuid); } private BleServiceBean(Parcel in) { readFromParcel(in); } private void writeToParcel(Parcel out) { out.writeString(name); out.writeString(uuid); } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel out,int flags) { writeToParcel(out); } public void readFromParcel(Parcel in) { name=in.readString(); uuid=in.readString(); } public static final Parcelable.Creator<BleServiceBean>CREATOR=new Parcelable.Creator<BleServiceBean>(){ @Override public BleServiceBean createFromParcel(Parcel source) { return new BleServiceBean(source); } @Override public BleServiceBean[]newArray(int size) { return new BleServiceBean[size]; } };}在Service中获取到相应数据后要传递给UI,有如下代码:
Bundle bundle=new Bundle(); bundle.putParcelableArrayList(BleConnectUtils.BLE_SERVICE_BEAN_LIST,beanList); msg.setData(bundle); mClients.get(i).send(msg);
在Client端获取数据的代码如下:
ArrayList<BleServiceBean>beanList=bundle.getParcelableArrayList(BleConnectUtils.BLE_SERVICE_BEAN_LIST);但是运行到此处时却出现如下错误:
FATAL EXCEPTION: main
Process: com.example.xxx.xx, PID: 1203
android.os.BadParcelableException: ClassNotFoundException when unmarshalling:
给出的出错原因如下:
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.xxx.xxx.bean.BleServiceBean" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /data/cust/lib, /system/lib]]
后面才发现原来是Android有两种不同的classloaders:framework classloader和apk classloader,其中framework classloader知道怎么加载android classes,apk classloader知道怎么加载你的代码,即可以知道你自定义的类,apk classloader继承自framework classloader,所以也知道怎么加载android classes。在应用刚启动时,默认class loader是apk classloader,但在系统内存不足应用被系统回收会再次启动,这个默认class loader会变为framework classloader了,所以对于自己的类会报ClassNotFoundException。
如果是在要传递的JavaBean中有其中一个Field继承自Parcelable,那么有很简单的处理方法,只要把类似rect = in.readParcelable(null);改为config = in.readParcelable(Rect.class.getClassLoader());
但是我们这里是直接传递一个List<BleServiceBean>,那要怎么办呢?
其实很简单,只需要在Client端读取Bundle中的数据之前加上如下一行代码:
bundle.setClassLoader(getClass().getClassLoader());这样就会使用apk classloader加载。
- Android中跨进程通信传递Parcelable对象时出现android.os.BadParcelableException: ClassNotFoundException when unmarsh
- android.os.BadParcelableException: ClassNotFoundException when unmarshalling
- android.os.BadParcelableException: ClassNotFoundException when unmarshalling
- android.os.BadParcelableException: ClassNotFoundException when unmarshalling
- android.os.BadParcelableException: ClassNotFoundException when unmarshalling
- Facebook Messenger crash . android.os.BadParcelableException: ClassNotFoundException when unmarshall
- android问题集锦之十二:android.os.BadParcelableException: ClassNotFoundException when unmarshalling
- android.os.BadParcelableException: ClassNotFondException when unmarshalling
- Android 使用AIDL跨进程通信(二)--传递自定义对象
- Android 使用AIDL跨进程通信(二)--传递自定义对象
- android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator错误解决
- Android中Parcelable接口跨进程传递复杂数据的应用
- Android中跨进程通信
- Android中Intent传递对象(Serializable,Parcelable)
- 进程间传递Parcelable对象出现ClassNotFoundException异常的解决方法:
- android 跨进程通信
- Android 跨进程通信
- Android跨进程通信
- C++ 排序函数 sort()的用法
- 容器的一些简单用法
- 开始
- android.database.sqlite.SQLiteException: Failed to change locale for db '/storage/sdcard0/xx/xxx.db'
- gdb 如何调试release版本
- Android中跨进程通信传递Parcelable对象时出现android.os.BadParcelableException: ClassNotFoundException when unmarsh
- hdu 1870 愚人节的礼物
- nyoj-915-+-字符串
- EMU8086 简单汇编程序&实现5个BYTES的数据相加
- (行为型模式九)备忘录模式
- C++中new、delete 与new[]、delete[]
- win8下安装pip
- 欢迎使用CSDN-markdown编辑器
- 单链表模板