我想将一个自定义对象从一个Activity传递到另一个Activity,这是我到目前为止所拥有的:
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)
....
我该怎么办?我在上面添加了一个catlog。
答案 0 :(得分:2)
你应该实现Parcelable,然后像
一样发送你的数组Intent.putParcelableArrayListExtra("array",yourarray);
和其他活动将其视为
Bundle bundle = getIntent().getExtras();
List<Guest> guestList = bundle.getParcelableArrayList("array");