Problem unmarshalling parcelables
Because this was not answered in "answer" but in comment I will post an answer:
As @Max-Gontar pointed you should use LocationType.class.getClassLoader() to get the correct ClassLoader and get rid of ClassNotFound exception, i.e.:
in.readParceleable(LocationType.class.getClassLoader());
Parcelable: Class not found when unmarshalling when calling from different App
You cannot pass parcelable class from one app to another?
Yes, you can. However, they have to be the same class. In other words, they need to have:
- The same base name (
Auxiliar
) - The same package name (
com.opp.App1.ui.main
) - The same fields
In your case, I am guessing that the package name is different. If you use the same package name in both apps — such as having both apps share a common library module that defines the class — then you will be in somewhat better shape.
However, bear in mind that users do not need to update App1 and App2 at the same time. Parcelable
is designed for use by framework classes, ones that are part of the firmware, where App1 and App2 are guaranteed to have the same implementation. If App1 has an old Auxiliar
and App2 has a new Auxiliar
, you may run into problems.
You can only pass primitive classes?
Passing anything that is defined in the Android SDK as Parcelable
is safe, because App1 and App2 will have the same class definition for those classes. So, for example, Bundle
is Parcelable
. You would still need a versioning system to deal with data changes, but at least you are in control over how to handle that.
Android Parcelable Issue - ClassNotFoundException when Unmarshalling
You should be using readList
, not readParcelable
. Read the contents into an existing list.
myProducts = mutableListOf<MyProduct>().apply {
parcel.readList(this, MyProduct::class.java.classLoader)
}
Also consider using the Parcelize plugin to save yourself some time.
Error with Parcelable object: Unmarshalling unknown type code *** at offset ***
Found the issue! It was something with one of the 3rd party libraries I was using. I had to read all the library documentation and I found that I needed to add some library specific code to the parcelable code of my object class.
Unmarshalling errors in Android app with custom parcelable classes
Android has two different classloaders: the framework classloader (which knows how to load Android classes) and the APK classloader (which knows how to load your code). The APK classloader has the framework classloader set as its parent, meaning it can also load Android classes.
Error #2 is likely caused by the Bundle using the framework classloader so it doesn't know of your classes. I think this can happen when Android needs to persist your Bundle and later restore it (for example when running out of memory in the background). You can fix this by setting the APK classloader on the bundle:
savedInstanceState.setClassLoader(getClass().getClassLoader());
Error #1 and #3 are more mysterious, are you perhaps writing null values in writeToParcel()
? Android doesn't like that very much I'm afraid.
issues with parcelable extension: getting unmarshalling unknown type code exception
well I'm posting this for those who may run into the same problem as me.
the problem was solved after adding the Creator
statement to the child classes.
for one of the child classes it would be like:
public static final Creator<FirstClass> CREATOR = new Creator<FirstClass>() {
@Override
public FirstClass createFromParcel(Parcel in) {
return new FirstClass(in);
}
@Override
public FirstClass[] newArray(int size) {
return new FirstClass[size];
}
};
hope this helps. 3>
unmarshalling exception with parcelable
I change
in.readList(listTest,List.class.getClassLoader());
and I got no error.
Unmarshalling exception on Arraylist with a custom Parcelable Object
The order you are writing and reading in is not correct. When serializing and deserializing objects using Parcels
you MUST read in the same order as you have written the objects.
See the corrected code below (I moved the last line in the QuestionAnswerDto
constructor):
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.id);
dest.writeString(this.question);
dest.writeString(this.typeOfQuestion);
dest.writeString(this.active);
dest.writeString(this.isMandatory);
dest.writeStringList(this.answers);
}
protected QuestionAnswerDto(Parcel in) {
this.id = in.readString();
this.question = in.readString();
this.typeOfQuestion = in.readString();
this.active = in.readString();
this.isMandatory = in.readString();
this.answers = new ArrayList<>();
in.readStringList(this.answers);
}
Related Topics
Meaning of the Import Statement in a Java File
When/Why to Call System.Out.Flush() in Java
Getting Java Gui to Open a Webpage in Web Browser
Why Can Not I Add Two Bytes and Get an Int and I Can Add Two Final Bytes Get a Byte
How to Get Eclipse to Use a Different Compiler Version for Java
Right Way to Implement Equals Contract
Is Addition of Byte Converts to Int Because of Java Language Rules or Because of Jvm
Java, Find Intersection of Two Arrays
Scanner Doesn't See After Space
Jtable, Rowfilter and Rowfilter.Entry
Absolute Positioning Graphic JPAnel Inside Jframe Blocked by Blank Sections
Should I Use Java Date and Time Classes or Go with a 3Rd Party Library Like Joda Time
Differencebetween ? and Object in Java Generics
Android Archive Library (Aar) VS Standard Jar
:App:Lintvitalrelease' Error When Generating Signed APK