2

我需要将自定义 Parcelable 对象作为 Extra 传递给另一个活动

我在这段代码中添加了额外的内容

Car myCarObject=getCar();
ArrayList<Car> parcelableExtra = new ArrayList<Car>();
                parcelableExtra.add(myCarObject);

Intent details = new Intent(Start.this, DetailsCar.class);
                details.putExtra("Car", parcelableExtra);
                startActivity(details);

我尝试在目标 Intent 中使用此代码加载额外内容

ArrayList<Car> parcelableExtra = new ArrayList<Car>();
        parcelableExtra = (ArrayList<Car>) getIntent().getExtras().getParcelable("Car");
        Car c=parcelableExtra.get(0);

但该应用程序因此错误堆栈而崩溃:

12-14 03:34:24.059: E/AndroidRuntime(16872): FATAL EXCEPTION: main
12-14 03:34:24.059: E/AndroidRuntime(16872): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.app.com/my.app.com.DetailsCar}: java.lang.RuntimeException: Parcel android.os.Parcel@4051ac20: Unmarshalling unknown type code 5505097 at offset 3144
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.os.Looper.loop(Looper.java:130)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.app.ActivityThread.main(ActivityThread.java:3687)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at java.lang.reflect.Method.invokeNative(Native Method)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at java.lang.reflect.Method.invoke(Method.java:507)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at dalvik.system.NativeStart.main(Native Method)
12-14 03:34:24.059: E/AndroidRuntime(16872): Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@4051ac20: Unmarshalling unknown type code 5505097 at offset 3144
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.os.Parcel.readValue(Parcel.java:1913)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.os.Parcel.readListInternal(Parcel.java:2092)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.os.Parcel.readArrayList(Parcel.java:1536)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at my.app.com.obj.Car.<init>(Car.java:361)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at my.app.com.obj.Car.<init>(Car.java:347)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at my.app.com.obj.Car$1.createFromParcel(Car.java:339)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at my.app.com.obj.Car$1.createFromParcel(Car.java:1)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.os.Parcel.readParcelable(Parcel.java:1981)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.os.Parcel.readValue(Parcel.java:1846)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.os.Parcel.readListInternal(Parcel.java:2092)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.os.Parcel.readArrayList(Parcel.java:1536)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.os.Parcel.readValue(Parcel.java:1867)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.os.Parcel.readMapInternal(Parcel.java:2083)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.os.Bundle.unparcel(Bundle.java:208)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.os.Bundle.getParcelable(Bundle.java:1100)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at my.app.com.DetailsCar.onCreate(DetailsCar.java:43)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-14 03:34:24.059: E/AndroidRuntime(16872):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-14 03:34:24.059: E/AndroidRuntime(16872):    ... 11 more

我的 Parcelable Car 对象是这个

public class Car implements Serializable, Parcelable {
    String description;
    String speed;
    String brake;
    String pro;

    public Car(String description, String speed, String brake,
            String pro) {
        super();
        this.description = description;
        this.speed = speed;
        this.brake = brake;
        this.pro = pro;
    }

    public Car() {

    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel arg0, int arg1) {
        arg0.writeString(description);
        arg0.writeString(speed);
        arg0.writeString(brake);
        arg0.writeString(pro);
    }

    public static final Parcelable.Creator<Car> CREATOR = new Parcelable.Creator<Car>() {
        public Car createFromParcel(Parcel in) {
            return new Car(in);
        }

        public Car[] newArray(int size) {
            return new Car[size];
        }
    };

    private Car(Parcel in) {
        description = in.readString();
        speed = in.readString();
        brake = in.readString();
        pro = in.readString();
        in.readArrayList(String.class.getClassLoader());
    }

}

我该如何解决这个问题?有什么建议吗?

4

1 回答 1

2

让我们看看两个关键功能:

@Override
public void writeToParcel(Parcel arg0, int arg1) {
    arg0.writeString(description);
    arg0.writeString(speed);
    arg0.writeString(brake);
    arg0.writeString(pro);
}


private Car(Parcel in) {
    description = in.readString();
    speed = in.readString();
    brake = in.readString();
    pro = in.readString();
    in.readArrayList(String.class.getClassLoader());
}

请注意,第一个将 4 个变量写入Parcel,然后读取 5 个。系统崩溃是因为 Parcel 中没有 ArrayList。放一个arrayList,你应该没问题。或者,您可以删除 read 功能,因为它甚至没有被使用(但我会考虑到您的功能中可能包含的功能可能比您包含的更多)

于 2012-12-14T03:14:19.200 回答