Kivylius
Kivylius

Reputation: 6547

Passing custom objects to Activities

I want to pass a custom object from one Activity to another, here is what I have so far:

Guests.java :

public class Guest extends Booking implements Serializable {
    public String SampleFunc(){
        return "hello";
    }
}

FirstActivity.this :

private ArrayList<Guest> guests = new ArrayList<Guest>();
....
this.guests.add(new Guest("Joe Blog","Standart","F01","22/22/2012"));
this.guests.add(new Guest("Herpa Bunn","Standart","F02","22/22/2012"));

Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("guests", this.guests);
startActivity(i);

SecondActivity.java :

ArrayList<Guest> basket =
         (ArrayList<Guest>).getIntent().getSerializableExtra("guests");
//this will crashes the program

LogCat :

05-03 18:30:34.216: W/System.err(492): java.lang.RuntimeException: Parcelable encountered IOException reading a Serializable object (name = ie.wit.hotelmanager.models.Guest)
05-03 18:30:34.216: W/System.err(492):  at android.os.Parcel.readSerializable(Parcel.java:1886)
05-03 18:30:34.216: W/System.err(492):  at android.os.Parcel.readValue(Parcel.java:1761)
05-03 18:30:34.216: W/System.err(492):  at android.os.Parcel.readSerializable(Parcel.java:1886)
05-03 18:30:34.216: W/System.err(492):  at android.os.Parcel.readValue(Parcel.java:1761)
05-03 18:30:34.216: W/System.err(492):  at android.os.Parcel.readSerializable(Parcel.java:1886)
05-03 18:30:34.216: W/System.err(492):  at android.os.Parcel.readValue(Parcel.java:1761)
05-03 18:30:34.216: W/System.err(492):  at android.os.Parcel.readListInternal(Parcel.java:1956)
05-03 18:30:34.216: W/System.err(492):  at android.os.Parcel.readArrayList(Parcel.java:1403)
05-03 18:30:34.216: W/System.err(492):  at android.os.Parcel.readValue(Parcel.java:1734)
05-03 18:30:34.226: W/System.err(492):  at android.os.Parcel.readMapInternal(Parcel.java:1947)
05-03 18:30:34.226: W/System.err(492):  at android.os.Bundle.unparcel(Bundle.java:169)
05-03 18:30:34.226: W/System.err(492):  at android.os.Bundle.getSerializable(Bundle.java:1126)
05-03 18:30:34.226: W/System.err(492):  at android.content.Intent.getSerializableExtra(Intent.java:3311)
05-03 18:30:34.226: W/System.err(492):  at ie.wit.hotelmanager.ViewGuestActivity.onCreate(ViewGuestActivity.java:25)
05-03 18:30:34.226: W/System.err(492):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-03 18:30:34.226: W/System.err(492):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-03 18:30:34.226: W/System.err(492):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-03 18:30:34.226: W/System.err(492):  at 05-03 18:30:34.216: W/System.err(492):   at android.os.Parcel.readListInternal(Parcel.java:1956)
....

What should I do? I added a catlog above.

Upvotes: 0

Views: 434

Answers (1)

Zaz Gmy
Zaz Gmy

Reputation: 4356

you should implement Parcelable and then sent your array like

Intent.putParcelableArrayListExtra("array",yourarray);

and other activity get it as

 Bundle bundle = getIntent().getExtras();
    List<Guest> guestList = bundle.getParcelableArrayList("array");

Upvotes: 2

Related Questions