gpt4 book ai didi

android - 尝试使用 Parcelable 传递列表对象时出错

转载 作者:行者123 更新时间:2023-11-29 14:58:12 26 4
gpt4 key购买 nike

我想使用 ParcelableIntent 传递一个 list object 但我遇到了一个问题:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to android.os.Parcelable

我的联系人.java:

public class Contact implements Parcelable {
private String id;
private String Name;
private String Fname;
private String Phone;
private String Email;


public Contact(String id, String Name, String Fname, String Phone, String Email) {
this.id = id;
this.Name = Name;
this.Fname = Fname;
this.Email = Email;
this.Phone = Phone;
}

public Contact(Parcel in) {
this.id = in.readString();
this.Name = in.readString();
this.Fname = in.readString();
this.Email = in.readString();
this.Phone = in.readString();
}

//Getter
public String getId() { return id; }

public String getName() {
return Name;
}

public String getFname() {
return Fname;
}

public String getEmail() {
return Email;
}

public String getPhone() {
return Phone;
}

//Setter

public void setName(String name) {
this.Name = name;
}

public void setFname(String fname) {
Fname = fname;
}

public void setEmail(String email) {
Email = email;
}

public void setPhone(String phone){ Phone = phone; }

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

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(Name);
dest.writeString(Fname);
dest.writeString(Phone);
dest.writeString(Email);

}
public static final Parcelable.Creator<Contact> CREATOR = new Parcelable.Creator<Contact>() {
public Contact createFromParcel(Parcel in)
{
return new Contact(in);
}
public Contact[] newArray(int size)
{
return new Contact[size];
}
};
}

还有我的 Intent :

List<Contact> mData = new ArrayList<Contact>();
Intent intent = new Intent(mContext, NewContactActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("CONTACT_ARRAY_LIST", mData);
intent.putExtras(bundle);
mContext.startActivity(intent);

我该如何解决这个问题? 非常感谢

最佳答案

Use putParcelableArrayList instead of putParcelable.
bundle.putParcelableArrayList("CONTACT_ARRAY_LIST", mData); // Be sure "mData" is not null here

关于android - 尝试使用 Parcelable 传递列表对象时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56448069/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com