我尝试从Intent获取Object Extra时出现RuntimeException

时间:2012-12-14 03:06:01

标签: android android-intent parcelable

我需要将自定义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 Object就是这个

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());
    }

}

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

1 个答案:

答案 0 :(得分: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放进去,你应该没问题。或者,您可以删除读取功能,因为它甚至似乎没有被使用(但我会考虑更多可能在您的功能中可能比您包括的更多)