0

Please help me how to solve this issue ?

Parcelable interface i was implemented in customer pojo object. please help me how to read customer object in activity 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);
}

}

In activity 1 used below code:

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:

 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 ?

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
nag
  • 647
  • 6
  • 25
  • 43

1 Answers1

0

Serialization, although supported, is not recommended for 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.

Do that, and then change the above line of code to:

in.readTypedList(accoutDetails, AccountDetails.CREATOR);

EDIT 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

public Customer implements Parcelable{
    private String name;
    private String phone;
    private List<AccountDetails> accoutDetails = new ArrayList<AccountDetails>();
MidasLefko
  • 4,499
  • 1
  • 31
  • 44
  • I tried with in.readTypedList(accoutDetails, AccountDetails.CREATOR). but parent data is coming. Account Details list is coming null – nag Aug 26 '15 at 13:35
  • As accountDetails is the list of AccountDetails. reference – nag Aug 26 '15 at 14:45
  • bundle.putParcelable("selected_customer", selected_row)-> in selected_row contains customer object+ list of account for that customer – nag Aug 26 '15 at 14:46
  • Customer selected_row=(Customer) parent.getAdapter().getItem(position); – nag Aug 26 '15 at 14:54
  • Its a customer : its one to many relationship: Once if click the customer we need to show account details of selected customer in activity 2 – nag Aug 26 '15 at 14:55
  • Yes now null exception is not coming but i am getting accountDetails is empty – nag Aug 26 '15 at 15:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88013/discussion-between-midaslefko-and-nag). – MidasLefko Aug 26 '15 at 15:09