" BadParcelableException:ClassNotFoundException的解组&LT时,MyClass的>"同时采用Parcel.read方法,有一个类加载器作为参数 [英] "BadParcelableException: ClassNotFoundException when unmarshalling <myclass>" while using Parcel.read method that has a ClassLoader as argument

查看:125
本文介绍了" BadParcelableException:ClassNotFoundException的解组&LT时,MyClass的>"同时采用Parcel.read方法,有一个类加载器作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个自定义的类 org.example.app.MyClass实现Parcelable ,我想写一个名单,其中,MyClass的> 来一个包裹。我做的编组

Given a custom class org.example.app.MyClass implements Parcelable, I want to write a List<MyClass> to a Parcel. I did the marshalling with

 List<MyClass> myclassList = ...
 parcel.writeList(myclassList);

每当我试图与解组的类

whenever I try to unmarshall the class with

 List<MyClass> myclassList = new ArrayList<MyClass>();
 parcel.readList(myclassList, null);

有一个。BadParcelableException:ClassNotFoundException的解组org.example.app.MyClass当除了

什么是错在这里?为什么找不到类?

What is wrong here? Why is the class not found?

推荐答案

不要解组自定义类,即,一个提供应用程序,而不是由Android框架,与当你给那得到的used框架类加载器的类加载器的参数。使用应用程序的类加载器:

Don't unmarshall a custom class, i.e. one provided by your application and not by the Android framework, with the framework class loader that get's used when you give null as Classloader argument. Use the application class loader:

parcel.readList(myclassList, getClass().getClassLoader());

每当 Parcel.read *()方法也有一个类加载器作为参数(如<一个href="http://developer.android.com/reference/android/os/Parcel.html#readList%28java.util.List,%20java.lang.ClassLoader%29"相对=nofollow> Parcel.readList(名单outVal,类加载器加载器) )和你想读从一个应用程序类包裹,使用应用程序类加载器,可以检索到的getClass()。getClassLoader()

Whenever a Parcel.read*() method also has a ClassLoader as argument (e.g. Parcel.readList(List outVal, ClassLoader loader)) and you want to read a application class from a Parcel, use the application class loader that can be retrieved with getClass().getClassLoader().

背景: Android自带了两个类加载器:系统类加载器,即能加载所有系统类,但被应用程序提供的那些。而应用程序类加载器,已设置系统类加载器作为父,因此是能够加载的所有类。如果你给空类加载器,<一个href="https://github.com/android/platform_frameworks_base/blob/android-4.3_r2.1/core/java/android/os/Parcel.java#L2075"相对=nofollow> Parcel.readParcelableCreator() ,<一个href="https://github.com/android/platform_frameworks_base/blob/android-4.3_r2.1/core/java/android/os/Parcel.java#L2091"相对=nofollow>将使用该框架的类加载器,引起<一href="https://github.com/android/platform_frameworks_base/blob/android-4.3_r2.1/core/java/android/os/Parcel.java#L2102"相对=nofollow> ClassNotFoundException的。

Background: Android comes with two class loaders: The system class loader, that is able to load all system classes but the the ones provided by your application. And the application class loader, which has set the system class loader as parent and therefore is able to load all classes. If you give null a class loader, Parcel.readParcelableCreator(), will use the framework class loader, causing in the ClassNotFoundException.

感谢alexanderblom提供提示导致我在正确的轨道

Thanks to alexanderblom for providing the hint that lead me on the right track

这篇关于&QUOT; BadParcelableException:ClassNotFoundException的解组&LT时,MyClass的&GT;&QUOT;同时采用Parcel.read方法,有一个类加载器作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆