传递parcelable对象的ArrayList时出错

时间:2018-02-10 20:38:25

标签: java android arraylist parcelable

我在Android Studio上的应用中发生了一个奇怪的错误,试图在活动之间传递可分配对象的ArrayList。可以传递objet Task但不可能使用putParcelableArrayListExtra传递任务的ArrayList。 我有这个错误

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)

这是我的课程任务:

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);
}
}

这是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);
        }

这是接收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");

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

Task(Parcel in)writeToParcel中元素的顺序必须相同。需要在其中一种方法中交换dureedescription

答案 1 :(得分:0)

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)
}

订单应该相同