【问题标题】:Android | java.lang.RuntimeException: Parcel android.os.Parcel@1219dd0f: Unmarshalling unknown type code 6881396 at offset 660安卓 | java.lang.RuntimeException:包裹 android.os.Parcel@1219dd0f:在偏移量 660 处解组未知类型代码 6881396
【发布时间】:2015-08-26 11:57:30
【问题描述】:

请帮我解决这个问题?

Parcelable 接口 i 在客户 pojo 对象中实现。请帮助我如何阅读活动 2 中的客户对象?

Customer.java

public Customer implements Parcelable{
    private String name;
    private String phone;
    private List<AccountDetails> accoutDetails;

/getter and setters

public int describeContents() {
    return 0;
}

public Customer(Parcel in) {
    name= in.readString();
    phone= in.readString();
    accoutDetails= new ArrayList<AccountDetails>();
    in.readList(accoutDetails,null);


}

public static final Parcelable.Creator<Customer> CREATOR = new Parcelable.Creator<Customer>() {
    public Customer createFromParcel(Parcel in) {
        return new Customer(in);
    }

    public Customer[] newArray(int size) {
        return new Customer[size];
    }
};

@Override
public void writeToParcel(Parcel dest, int arg1) {
    // TODO Auto-generated method stub
    dest.writeString(this. name);
    dest.writeString(this.phone);

    dest.writeList(accoutDetails);
}

}

在 activity 1 中使用以下代码:

Customer selected_row=(Customer) parent.getAdapter().getItem(position);
Intent intent = new Intent(getApplicationContext(), Activity2.class);
Bundle bundle = new Bundle();
bundle.putParcelable("selected_customer", selected_row);
intent.putExtras(bundle);
startActivity(intent);

活动 2:

 Customer cust_object = getBundle.getParcelable("selected_customer");

请找出以下例外:

java.lang.RuntimeException: Parcel android.os.Parcel@1219dd0f: Unmarshalling unknown type code 6881396 at offset 660
at android.os.Parcel.readValue(Parcel.java:2228)
at android.os.Parcel.readListInternal(Parcel.java:2526)
at android.os.Parcel.readList(Parcel.java:1661)

请帮助我如何在活动 2 中读取客户对象?

【问题讨论】:

  • 发布完整的 logcat 错误
  • 将其编辑到您的问题中,而不是作为评论。

标签: android parcelable runtimeexception


【解决方案1】:

虽然支持序列化,但不推荐用于 Android。参见例如this post。

我认为你的问题出在这行代码上:

in.readList(accoutDetails,null);

由于 accountDetails 是自定义 AccountDetails 对象的列表,因此您需要 AccountDetails 来实现 Parcelable。

这样做,然后将上面的代码行更改为:

in.readTypedList(accoutDetails, AccountDetails.CREATOR);

编辑 1

另外,改变:

public Customer(Parcel in) {
    name= in.readString();
    phone= in.readString();
    accoutDetails= new ArrayList<AccountDetails>();
    in.readTypedList(accoutDetails,null);
}

到:

public Customer(Parcel in) {
    name= in.readString();
    phone= in.readString();
    in.readTypedList(accoutDetails, AccountDetails.CREATOR);
}

然后改变:

@Override
public void writeToParcel(Parcel dest, int arg1) {
    // TODO Auto-generated method stub
    dest.writeString(this. name);
    dest.writeString(this.phone);

    dest.writeList(accoutDetails);
}

收件人:

@Override
public void writeToParcel(Parcel dest, int arg1) {
    dest.writeString(this.name);
    dest.writeString(this.phone);
    dest.writeTypedList(accoutDetails);
}

编辑 2

public Customer implements Parcelable{
    private String name;
    private String phone;
    private List<AccountDetails> accoutDetails = new ArrayList<AccountDetails>();

【讨论】:

  • 我尝试使用 in.readTypedList(accoutDetails, AccountDetails.CREATOR)。但是父数据来了。帐户详细信息列表即将为空
  • 因为 accountDetails 是 AccountDetails 的列表。参考
  • bundle.putParcelable("selected_customer", selected_row)-> 在 selected_row 中包含客户对象+该客户的帐户列表
  • 客户 selected_row=(客户) parent.getAdapter().getItem(position);
  • 它是一个客户:它的一对多关系:如果单击客户,我们需要在活动 2 中显示所选客户的帐户详细信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-10
  • 2020-05-06
  • 1970-01-01
  • 1970-01-01
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多