简体   繁体   中英

Error passing ArrayList of parcelable object

I have a strange error in my app on Android Studio trying to pass ArrayList of parcelable objects between activities. It's possible to pass the objet Task but impossible to pass an ArrayList of task with putParcelableArrayListExtra. I have this error

02-10 20:27:46.567: E/AndroidRuntime(12683): java.lang.RuntimeException: Parcel android.os.Parcel@9d0525e8: Unmarshalling unknown type code 2097253 at offset 128
02-10 20:27:46.567: E/AndroidRuntime(12683):    at android.os.Parcel.readValue(Parcel.java:2080)
02-10 20:27:46.567: E/AndroidRuntime(12683):    at android.os.Parcel.readListInternal(Parcel.java:2343)
02-10 20:27:46.567: E/AndroidRuntime(12683):    at android.os.Parcel.readArrayList(Parcel.java:1703)
02-10 20:27:46.567: E/AndroidRuntime(12683):    at android.os.Parcel.readValue(Parcel.java:2034)
02-10 20:27:46.567: E/AndroidRuntime(12683):    at android.os.Parcel.readArrayMapInternal(Parcel.java:2314)
02-10 20:27:46.567: E/AndroidRuntime(12683):    at android.os.Bundle.unparcel(Bundle.java:249)
02-10 20:27:46.567: E/AndroidRuntime(12683):    at android.os.Bundle.getParcelableArrayList(Bundle.java:1250)
02-10 20:27:46.567: E/AndroidRuntime(12683):    at android.content.Intent.getParcelableArrayListExtra(Intent.java:4680)
02-10 20:27:46.567: E/AndroidRuntime(12683):    at com.example.augus.tp2.NewActivity$1.onClick(NewActivity.java:51)

This is my class Task:

public class Task implements Parcelable {
public String nom;
private int duree;
private String description;
private String categorie;

public Task(String nom, int duree, String description, String categorie){
    this.nom=nom;
    this.duree=duree;
    this.description=description;
    this.categorie=categorie;

}


protected Task(Parcel in) {
    nom = in.readString();
    duree = in.readInt();
    description = in.readString();
    categorie = in.readString();
}

public static final Creator<Task> CREATOR = new Creator<Task>() {
    @Override
    public Task createFromParcel(Parcel in) {
        return new Task(in);
    }

    @Override
    public Task[] newArray(int size) {
        return new Task[size];
    }
};

public String getNom(){
    return this.nom;
}

public String getCategorie(){
    return this.categorie;
}

public int getDuree(){
    return this.duree;
}

public String getDescription(){
    return this.description;
}


@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(nom);
    dest.writeString(description);
    dest.writeInt(duree);
    dest.writeString(categorie);
}
}

This is the function OnClick passing the ArrayList

 public void onClick(View v) {

            Intent secondActivity = new Intent(MainActivity.this, NewActivity.class);

            ArrayList<Task> list=new ArrayList<Task>();
            list.add(task1);
            list.add(task2);
            list.add(task3);
            list.add(task4);
            list.add(task5);
            list.add(task6);
            list.add(task7);
            secondActivity.putParcelableArrayListExtra("task",list);
            startActivity(secondActivity);
        }

And this is the function receiving the ArrayList

Intent secondeActivite = new Intent(NewActivity.this, MainActivity2.class);
            String activitenom = nom.getText().toString();
            String activitedescription=description.getText().toString();
            String activitecategorie=categorie.getText().toString();
            Intent i = getIntent();
            Task tache=new Task(activitenom,10,activitedescription,activitecategorie);

            ArrayList<Task> tacheliste=i.getParcelableArrayListExtra("task");

Thank you for your help

The order of your elements in Task(Parcel in) and writeToParcel have to be in the same order. duree and description needs to be swapped in one of the methods.

Sorry the code is in `kotlin`

constructor(source: Parcel) : this(
        source.readString(),
        source.readInt(),
        source.readString(),
        source.readString()
)

override fun describeContents() = 0

override fun writeToParcel(dest: Parcel, flags: Int) = with(dest) {
    writeString(nom)
    writeInt(duree)
    writeString(description)
    writeString(categorie)
}

The order should be the same

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM