[英]Android | java.lang.RuntimeException: Parcel android.os.Parcel@1219dd0f: Unmarshalling unknown type code 6881396 at offset 660
Please help me how to solve this issue ? 请帮我如何解决这个问题?
Parcelable interface i was implemented in customer pojo object. 我在客户pojo对象中实现了可打包接口。 please help me how to read customer object in activity 2 ? 请帮助我如何阅读活动2中的客户对象?
Customer.java 客户.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);
}
}
In activity 1 used below code: 在活动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);
Activity 2: 活动2:
Customer cust_object = getBundle.getParcelable("selected_customer");
Please find the below exception: 请找到以下异常:
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)
Please help me how to read customer object in activity 2 ? 请帮助我如何阅读活动2中的客户对象?
Serialization, although supported, is not recommended for Android. 虽然支持序列化,但不建议Android使用。 See for example this post . 例如,参见这篇文章 。
I think that your issue is on this line of code: 我认为您的问题在以下代码行上:
in.readList(accoutDetails,null);
Since accountDetails is a List of custom AccountDetails objects you need AccountDetails to also implement Parcelable. 由于accountDetails是自定义AccountDetails对象的列表,因此需要AccountDetails才能实现Parcelable。
Do that, and then change the above line of code to: 这样做,然后将上面的代码行更改为:
in.readTypedList(accoutDetails, AccountDetails.CREATOR);
EDIT 1 编辑1
Also, change: 另外,更改:
public Customer(Parcel in) {
name= in.readString();
phone= in.readString();
accoutDetails= new ArrayList<AccountDetails>();
in.readTypedList(accoutDetails,null);
}
to: 至:
public Customer(Parcel in) {
name= in.readString();
phone= in.readString();
in.readTypedList(accoutDetails, AccountDetails.CREATOR);
}
And change: 并更改:
@Override
public void writeToParcel(Parcel dest, int arg1) {
// TODO Auto-generated method stub
dest.writeString(this. name);
dest.writeString(this.phone);
dest.writeList(accoutDetails);
}
To: 至:
@Override
public void writeToParcel(Parcel dest, int arg1) {
dest.writeString(this.name);
dest.writeString(this.phone);
dest.writeTypedList(accoutDetails);
}
EDIT 2 编辑2
public Customer implements Parcelable{
private String name;
private String phone;
private List<AccountDetails> accoutDetails = new ArrayList<AccountDetails>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.