Android | Unmarshalling Unknown Type when using Parcelable Data in Bundles

I have a problem with Parcelable Data in an ArrayList sending two actions using Android.Bundle

I have two operations (A and B).

In class Aaa.class:

ArrayList<Model> mModelList = new ArrayList<Model> //Fill ArrayList with a few Model-Objects Bundle mBundle = new Bundle; Intent mIntent = new Intent(Aaaa.this, Bbbb.class); mBundle.putParcelableArrayList("models", mModelList); mIntent.putExtras(mBundle); startActivity(mIntent); 

In Bbbb.class:

 Bundle mBundle = getIntent().getExtras(); ArrayList<Model> = mBundle.getParcelableArrayList("models"); 

Model.class implements Parcelable.

So the problem. When I populate an ArrayList (in Aaa.class) and put it in a Bundle, I see that the Bundle contains varios objects from the list. When I then try to populate the list in Bbbb.class, an exception is thrown.

  ERROR/AndroidRuntime(11109): FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{test/test.activities.Bbbb}: java.lang.RuntimeException: Parcel android.os.Parcel@405585d0 : Unmarshalling unknown type code 7667810 at offset 144 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) at android.app.ActivityThread.access$1500(ActivityThread.java:117) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:3687) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@405585d0 : Unmarshalling unknown type code 7667810 at offset 144 at android.os.Parcel.readValue(Parcel.java:1913) at android.os.Parcel.readListInternal(Parcel.java:2092) at android.os.Parcel.readArrayList(Parcel.java:1536) at android.os.Parcel.readValue(Parcel.java:1867) at android.os.Parcel.readMapInternal(Parcel.java:2083) at android.os.Bundle.unparcel(Bundle.java:208) at android.os.Bundle.getParcelableArrayList(Bundle.java:1144) at test.activities.Bbbb.onCreate(Bbbb.java:52) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) ... 11 more 

Line52 -

 ArrayList<Model> = mBundle.getParcelableArrayList("models"); 

I have absolutely no idea where the problem is here, Model.class works fine with other Bundle - Intents.

Update:

The next class of model.

 public class Model implements Parceleble{ private String name; private String address; public Model(Parcel parcel){ } public Model(){ } public Model(String name, String address){ this.name = name; this.address = address; } //Getter and Setter //equals, HashCode, toString (autoGenerated from Idea) @Override public void writeToParcel(Parcel parcel, int i){ parcel.writeString(name); parcel.writeString(address); } public void readFromParcel(Parcel parcel){ this.name = parcel.readString(); this.address = parcel.readString(); } public static Parcelable.Creator<Model> CREATOR = new Parcelable.Creator<Model>(){ @Override public Model createFromParcel(Parcel parcel){ return new Model(parcel); } @Override public Model[] new Array(int size){ return new Model[size] } }; } 
+6
source share
2 answers

Your implementation of Parceleble doesn’t look quite right for me, look at the sample code in the API to find out which method / constructor you need to override:

 ... ... // ==================== Parcelable ==================== public int describeContents() { return 0; } public void writeToParcel(Parcel parcel, int flags) { parcel.writeString(name); parcel.writeString(address); } private Model(Parcel in) { name = in.readString(); address = in.readString(); } public static final Parcelable.Creator<Model> CREATOR = new Parcelable.Creator<Model>() { public Model createFromParcel(Parcel in) { return new Model(in); } public Model[] newArray(int size) { return new Model[size]; } }; ... ... 

Try this code and see if it helps.

+5
source

I spent a lot of time to find out what it is, and I think there is a mistake. Let me explain :

If your model has string fields, Android cannot restore them very well using the method

 Parcel.readString() 

He tries to convert the string and give you an unknown type, such as an unfamiliar character. Just by performing a test, if you use an arbitrary model without string fields, it looks great.

I do not know what to do and why this happens with string fields.

To solve this problem, I thought of passing the list of model objects to the next action using the database, maybe this is not a good idea, but I have no other.

+1
source

Source: https://habr.com/ru/post/910817/


All Articles