Unmarshalling errors in an Android application with custom cool classes

For my Android application, I get several non-sorting errors, although I think I have done everything necessary to save and load objects correctly using Parcelable s. Can you tell me what happened to my code?

Error 1:

 java.lang.RuntimeException: Unable to start activity ComponentInfo Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@41279860: Unmarshalling unknown type code 6619241 at offset 1372 at android.os.Parcel.readValue(Parcel.java:1922) at android.os.Parcel.readMapInternal(Parcel.java:2094) at android.os.Bundle.unparcel(Bundle.java:223) at android.os.Bundle.getParcelable(Bundle.java:1158) at android.app.Activity.onCreate(Activity.java:860) at my.app.package.PlayComputer.onCreate(PlayComputer.java:1012) at android.app.Activity.performCreate(Activity.java:4465) 

Line 1012 in MyActivity is a call to super.onCreate(savedInstanceState); in Activity onCreate() .

 protected void onSaveInstanceState(Bundle savedInstanceState) { if (myObject == null) { savedInstanceState.putParcelable("myObject", null); } else { savedInstanceState.putParcelable("myObject", myObject); } savedInstanceState.putInt(...); savedInstanceState.putString(...); savedInstanceState.putBoolean(...); super.onSaveInstanceState(savedInstanceState); } 

myObject has a class myObject , which has the following methods:

 public void writeToParcel(Parcel out, int flags) { out.writeIntArray(...); out.writeInt(...); out.writeStringArray(...); out.writeString(...); out.writeParcelableArray(..., flags); } public static final Parcelable.Creator<MyObject> CREATOR = new Parcelable.Creator<MyObject>() { public MyObject createFromParcel(Parcel in) { try { if (in == null) { return null; } else { return new MyObject(in); } } catch (Exception e) { return null; } } public MyObject[] newArray(int size) { return new MyObject[size]; } }; private MyObject(Parcel in) { in.readIntArray(...); ... = in.readInt(); in.readStringArray(...); ... = in.readString(); ... = (OtherObject[]) in.readParcelableArray(OtherObject.class.getClassLoader()); } 

Error 2:

 java.lang.RuntimeException: Unable to start activity ComponentInfo Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: at android.os.Parcel.readParcelable(Parcel.java:1971) at android.os.Parcel.readValue(Parcel.java:1859) at android.os.Parcel.readMapInternal(Parcel.java:2099) at android.os.Bundle.unparcel(Bundle.java:223) at android.os.Bundle.getParcelable(Bundle.java:1158) at android.app.Activity.onCreate(Activity.java:905) at my.app.package.PlayComputer.onCreate(SourceFile:1012) 

The same files and classes.

Error 3:

 java.lang.RuntimeException: Unable to start activity ComponentInfo Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@4051aff8: Unmarshalling unknown type code 7340149 at offset 1276 at android.os.Parcel.readValue(Parcel.java:1913) at android.os.Parcel.readMapInternal(Parcel.java:2083) at android.os.Bundle.unparcel(Bundle.java:208) at android.os.Bundle.getParcelable(Bundle.java:1100) at my.app.package.PlayComputer.onCreate(SourceFile:1111) 

This time, the calling line (1111) will be as follows:

 myObject = (MyObject) savedInstanceState.getParcelable("myObject"); 
+13
android exception parcelable runtimeexception
Dec 21
source share
3 answers

Android has two different class loaders: the classloader class (which knows how to load Android classes) and the APK class loader (which knows how to load your code). The APK classloader has a classloader class that is its parent, that is, it can also load Android classes.

Mistake # 2 is most likely caused by the Bundle using the classloader class, so it does not know your classes. I think this can happen when Android needs to save your bundle and then restore it (for example, when there is not enough memory in the background). You can fix this by installing the APK class loader bundled:

savedInstanceState.setClassLoader (GetClass () getClassLoader ().);

Mistake # 1 and # 3 are more mysterious, maybe you are writing null values ​​in writeToParcel() ? Android is not very like, I'm afraid.

+35
Dec 30 '12 at 7:12
source share

In appearance, createFromParcel and newArray should be redefined as follows:

 public static final Parcelable.Creator<MyObject> CREATOR = new Parcelable.Creator<MyObject>() { @Override public MyObject createFromParcel(Parcel in) { MyObject myObj = new MyObject(); myObj.intArray = in.readIntArray(...); myObj.intValue = in.readInt(...); // .... // IN THE SAME ORDER THAT IS WRITTEN OUT AS PER writeToParcel! // return myObj; } @Override public MyObject[] newArray(int size) { return new MyObject[size]; } }; 

Edit:

I forgot to mention that in order for the above to work, there had to be an empty constructor!

 public MyObject(){} 
+2
Dec 21
source share

Before reading from the ClassLoader package set

bundle.setClassLoader (GetClass () getClassLoader ().);

it saved my time!

0
Nov 19 '16 at 17:18
source share



All Articles