简体   繁体   English

传递可包裹对象的ArrayList时出错

[英]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. 我在Android Studio上的应用中尝试在活动之间传递可包裹对象的ArrayList时遇到一个奇怪的错误。 It's possible to pass the objet Task but impossible to pass an ArrayList of task with putParcelableArrayListExtra. 可以传递objet任务,但不能通过putParcelableArrayListExtra传递任务的ArrayList。 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 这是函数OnClick传递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 这是接收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. Task(Parcel in)writeToParcel中的元素顺序必须相同。 duree and description needs to be swapped in one of the methods. dureedescription需要的方法之一被交换。

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 顺序应该一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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