【问题标题】:Error when try to pass list object by using Parcelable尝试使用 Parcelable 传递列表对象时出错
【发布时间】:2023-03-26 19:59:01
【问题描述】:

我想通过使用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);

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

【问题讨论】:

    标签: android bundle parcelable


    【解决方案1】:
    Use putParcelableArrayList instead of putParcelable.
    bundle.putParcelableArrayList("CONTACT_ARRAY_LIST", mData); // Be sure "mData" is not null here
    

    【讨论】:

    • java.util.ArrayList cannot be cast to android.os.Parcelable 继续出错
    • 我确定它不为空
    • @JasonMomoa 这对我来说很奇怪,因为它在我的代码中工作,您能否更改 List -> ArrayList。希望对你有帮助
    • @JasonMomoa 欲了解更多信息,您也可以参考此链接:stackoverflow.com/questions/39469056/…
    • 我不知道为什么它不起作用T_T,对此感到厌倦,我全部更改为ArrayList,但它也不起作用
    【解决方案2】:

    ArrayList 是不可包裹的,因为错误清楚地表明了这一点。但它是可序列化的as documented,所以就这样吧:

    bundle.putSerializable("CONTACT_ARRAY_LIST", mData);
    

    您也可以使用putParcelableArrayList():

    bundle.putParcelableArrayList("CONTACT_ARRAY_LIST", mData);
    

    【讨论】:

    • 我正在使用 Parcelable,现在有了您的回答,我必须更改为实现 Serializable。不能使用 Parcelable 的其他解决方案吗?
    • 您可以改用putParcelableArrayList()。我编辑了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 2014-04-25
    相关资源
    最近更新 更多