【发布时间】:2023-03-26 19:59:01
【问题描述】:
我想通过使用Parcelable 和Intent 传递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);
我该如何解决这个问题? 非常感谢您
【问题讨论】:
标签: android bundle parcelable