I have a Problem with Parcelable Data in an ArrayList sending via two Activities using Android.Bundle
I have two Activities (A and B).
In Aaaa.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");
The Model.class is implementing Parcelable.
So, the Problem is. When I fill the ArrayList (in Aaaa.class) and put it to the Bundle, I can see that the Bundle contains the varios Objects from the List. When I then try to fill the List in Bbbb.class a 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 is
ArrayList<Model> = mBundle.getParcelableArrayList("models");
I have absolutly no Idea where is the Problem here, the Model.class is working fine with other Bundle - Intents.
Update:
The Model Class as followed.
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]
}
};
}
Your Parceleble implementation doesn't look quite correct to me, check out the sample code in API to see what are the required method/constructor that need to be overridden:
... ...
// ==================== 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.
I have spent a long time to discover what is that and I think there is a bug. Let me explain:
If your Model has string fields the Android can't recovery them very well on the method
Parcel.readString()
He tries to convert the string and give to you an unknown type like an unknow character. Just doing a test, if you use a parcelable model without string fields, it occours perfectly.
I don't know what to do and why it occours with string fields.
To solve the problem, I thought passing a list of model objects to the next activity using database, perhaps it don't be the better idea but I don't have another.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With