0

我想将自定义对象从一个 Activity 传递到另一个,这是我目前所拥有的:

客人.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);

第二活动.java:

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

日志猫:

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)
....

我应该怎么办?我在上面添加了目录。

4

1 回答 1

2

你应该实现 Parcelable 然后发送你的数组

Intent.putParcelableArrayListExtra("array",yourarray);

和其他活动得到它

 Bundle bundle = getIntent().getExtras();
    List<Guest> guestList = bundle.getParcelableArrayList("array");
于 2012-05-03T18:57:02.803 回答