Android. OS. BadParcelableException: ClassNotFoundException when unmarsh, dllnotfoundexception

Source: Internet
Author: User

Android. OS. BadParcelableException: ClassNotFoundException when unmarsh, dllnotfoundexception

Reprinted please indicate the source: http://blog.csdn.net/bettarwang/article/details/45315091

According to the Google development documentation, MessengerService rather than AIDL is recommended for cross-process communication. Recently, the MessengerService method was adopted for implementing a cross-process Service.

Then a class is defined:

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];              }            };}
After obtaining the corresponding data in the Service, it must be passed to the UI. The following code is available:

 Bundle bundle=new Bundle();                bundle.putParcelableArrayList(BleConnectUtils.BLE_SERVICE_BEAN_LIST,beanList);                msg.setData(bundle);                mClients.get(i).send(msg);

The code for obtaining data from the Client is as follows:

ArrayList<BleServiceBean>beanList=bundle.getParcelableArrayList(BleConnectUtils.BLE_SERVICE_BEAN_LIST);
However, the following error occurs during running:

Fatal exception: main
Process: com. example. xxx. xx, PID: 1203
Android. OS. BadParcelableException: ClassNotFoundException when unmarshalling:

The error causes are as follows:

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]

Later I found that Android has two different classloaders: framework classloader and apk classloader. The framework classloader knows how to load android classes and apk classloader knows how to load your code, you can know your custom class. apk classloader inherits from framework classloader, so you also know how to load android classes. When the application is started, the default class loader is apk classloader. However, when the system memory is insufficient, the application is recycled and started again. The default class loader is changed to framework classloader, therefore, ClassNotFoundException is reported for your class.

If one of the fields in the JavaBean to be passed inherits from Parcelable, there is a very simple solution, as long as rect = in. readParcelable (null); changed to config = in. readParcelable (Rect. class. getClassLoader ());

But here we will directly pass a List <BleServiceBean>. What should we do?

In fact, it is very easy to add the following code before the Client reads the data in the Bundle:

bundle.setClassLoader(getClass().getClassLoader());
In this way, the apk classloader will be used for loading.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.