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);
there is an "BadParcelableException: ClassNotFoundException when unmarshalling org.example.app.MyClass"
exception.
What is wrong here? Why is the class not found?
I believe a more correct form of this would be:
Because here you are explicitly using the class loader for the List's generic type.
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 is used when you give
null
as the ClassLoader argument. Use the application class loader:Whenever a
Parcel.read*()
method also has a ClassLoader as an argument (e.g.Parcel.readList(List outVal, ClassLoader loader)
) and you want to read an application class from aParcel
, use the application class loader that can be retrieved withgetClass().getClassLoader()
.Background: Android comes with two class loaders: the system class loader, that is able to load all system classes but the ones provided by your application; and the application class loader, which has set the system class loader as its parent and therefore is able to load all classes. If you give null as the class loader,
Parcel.readParcelableCreator()
will use the framework class loader, causing a ClassNotFoundException.Thanks to alexanderblom for providing the hint that led me on the right track.
I have faced the same problem and instead of parcleable I used bundle
And in my intent class I used this to retrieve the value:
Hope it will be helpful for some.
You can get this error if you subclass a custom View incorrectly.
Assume you are subclassing
BottomNavigationView
and you want to add saved state to the superstate inonSaveInstanceState()
.An incorrect implementation of the Parcelable boilerplate (copied from another class or a template) would look like this:
This would not work as the superstate from
BottomNavigationView
requires a classloader. Instead you should carefully inspect theSavedState
class fromBottomNavigationView
and use the correctClassLoaderCreator
rather thanCreator
:Note that extending
android.support.v4.view.AbsSavedState
may be a better choice thanBaseSavedState
orandroid.view.AbsSavedState
since it will allow you to pass a class loader to the superclass: