How to fix Unmarshalling unknown type code XXX at offset YYY in Android?
Solution 1
It's because of Proguard obfuscation - it processed Parcelable. solution: Proguard causing RuntimeException (Unmarshalling unknown type code) in Parcelable class
Solution 2
There's some rules for write and read parcelables.
1- Be careful about type mismatch. If you write int, don't try to read long etc.
2- Be careful about order of writes and reads. Objects must be at the same sort order when reading and writing.
Both of these can cause "Unmarshalling unknown type code".
Solution 3
If a type of List is added, it should be:
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeList(this.mList);
and unparcel it using the class type like this:
protected MyClass(Parcel in) {
super(in);
this.mList = new ArrayList<>();
in.readList(this.mList, MyRequiredClass.class.getClassLoader());
Solution 4
Just remove all parcelable methods and generate them again with valid sequence
Solution 5
In my case it is fixed after upgrading support library to 23.2.1
4ntoine
Updated on July 09, 2022Comments
-
4ntoine almost 2 years
I'm having app crash on resume because of Unmarshalling exception. I've checked all the Serializables have constructor with no parameters and even checked all the serializables using ObjectStream (save to file and load from file). How can i understand actual class type for parcelable offset that cause exception:
Parcel android.os.Parcel@42209460: Unmarshalling unknown type code 2131165303 at offset 3748 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2080) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2105) at android.app.ActivityThread.access$600(ActivityThread.java:136) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4876) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:804) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:571) at com.kdgdev.xtension.core.XtensionMain.main(XtensionMain.java:91) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@42209460: Unmarshalling unknown type code 2131165303 at offset 3748 at android.os.Parcel.readValue(Parcel.java:2032) at android.os.Parcel.readSparseArrayInternal(Parcel.java:2255) at android.os.Parcel.readSparseArray(Parcel.java:1687) at android.os.Parcel.readValue(Parcel.java:2022) at android.os.Parcel.readMapInternal(Parcel.java:2226) at android.os.Bundle.unparcel(Bundle.java:223) at android.os.Bundle.getSparseParcelableArray(Bundle.java:1232) at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1690) at android.app.Activity.onRestoreInstanceState(Activity.java:999) at com.actionbarsherlock.app.SherlockFragmentActivity.onRestoreInstanceState(Unknown Source) at name.myname.android.app.ui.MainActivity.onRestoreInstanceState(Unknown Source) at android.app.Activity.performRestoreInstanceState(Activity.java:971) at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1130) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2058) ... 12 more
Saved data consists of
Bundle
s andSerializable
s and all of them look good.I'm going to do the next:
try { // unmarshalling } catch (Throwable t) { // look offset }
How can i understand what type is actually for Parcelable offset?
-
Srneczek over 8 yearsSame problem but it happens while debuging so this is not solution for me - another idea?
-
ab.helly over 8 years@Srneczek have you found a solution? Did it happen only on 6.x or other versions too?
-
Srneczek over 8 years@ab.helly no, I cant remember what problem was it - If I come across this problem again I can answer you but I think I used another approach to avoid unmarshalling problems
-
worked about 8 yearsFor what it's worth, it might be your implementation as well. I had this issue recently with a custom ViewGroup I wrote. I did not make the CREATOR static, so it was throwing this error on rotation when the Activity was destroyed. It was easy to replicate by turning on "Don't keep activities" in the Developer Options.
-
Sufian about 8 years@worked it might be due to some reading from/writing to Parcel. In my case, I was writing to Parcel but not reading from it. It resulted in a mess when instance state was restoring.
-
TWiStErRob about 8 yearsI wonder what was the problem, because I don't see any related issue at developer.android.com/topic/libraries/support-library/…
-
kostyabakay over 7 yearsThis was my solution: Be careful about order of writes and reads. Objects must be at the same sort order when reading and writing.
-
Lewis Wheeler about 6 yearsIf your getting this error after adding List into your class which implements Parcelable then this was what fixed it for me.
-
zhangliang about 5 years3 - Be careful about order of writes and reads in field of Parcelable type if have one . The field of Parcelable type must be at the same sort order when reading and writing.
-
zhangliang about 5 yearsBecause the exception may be occurred where not exactly the stack trace show. For example: the stack trace show field A cause the exception, but in fact it is field B of of parcelable type cause the exception.
-
rupinderjeet over 3 yearsFo me, it was about wrong declaration of CREATOR field in one of the custom views.
-
Kommineni Veeranayana about 3 yearsI wasted couple of hours to fix this issue. I faced same issue you suggestion saved me read and write order should be same