getParcelableExtra()在传递对象时启动活动后破碎

时间:2013-08-10 20:48:55

标签: android class object android-intent parcelable

我试图在活动之间传递一个对象,但我的应用程序崩溃了:

这是我的类(通过意图的对象):

public class item implements Parcelable {
private int id;
private String title;
private String desc;
private double lat;
private double lon;
private String pub;
private int p;
private int n;

public item(int id, String title, String desc, double lat, double lon, String pub, int p, int n) {
    super();
    this.id = id;
    this.title = title;
    this.desc = desc;
    this.lat = lat;
    this.lon = lon;
    this.pub = pub;
    this.p = p;
    this.n = n;
}
}

我使用对象调用intent(对象在列表中):

Intent.putExtra("thing",markers.get(i));
startActivity(Intent);

我在目的地活动中获得Extra:

item item = getIntent().getParcelableExtra("thing");

我的logcat:

08-10 23:47:21.009: W/dalvikvm(30373): threadid=1: thread exiting with uncaught exception (group=0x4101b2a0)
08-10 23:47:21.009: E/AndroidRuntime(30373): FATAL EXCEPTION: main
08-10 23:47:21.009: E/AndroidRuntime(30373): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.free/com.example.free.ItemView}: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called  CREATOR on class com.example.free.item
08-10 23:47:21.009: E/AndroidRuntime(30373):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at android.app.ActivityThread.access$700(ActivityThread.java:134)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at android.os.Looper.loop(Looper.java:137)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at android.app.ActivityThread.main(ActivityThread.java:4856)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at java.lang.reflect.Method.invokeNative(Native Method)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at java.lang.reflect.Method.invoke(Method.java:511)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at dalvik.system.NativeStart.main(Native Method)
08-10 23:47:21.009: E/AndroidRuntime(30373): Caused by: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called  CREATOR on class com.example.free.item
08-10 23:47:21.009: E/AndroidRuntime(30373):    at android.os.Parcel.readParcelable(Parcel.java:2086)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at android.os.Parcel.readValue(Parcel.java:1965)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at android.os.Parcel.readMapInternal(Parcel.java:2226)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at android.os.Bundle.unparcel(Bundle.java:223)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at android.os.Bundle.getParcelable(Bundle.java:1165)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at android.content.Intent.getParcelableExtra(Intent.java:4435)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at com.example.free.ItemView.onCreate(ItemView.java:21)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at android.app.Activity.performCreate(Activity.java:5047)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-10 23:47:21.009: E/AndroidRuntime(30373):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
08-10 23:47:21.009: E/AndroidRuntime(30373):    ... 11 more

修改

全班同学:

import android.os.Parcel;
import android.os.Parcelable;

public class item implements Parcelable {
private int id;
private String title;
private String desc;
private double lat;
private double lon;
private String pub;
private int p;
private int n;

public item(int id, String title, String desc, double lat, double lon, String pub, int p, int n) {
    super();
    this.id = id;
    this.title = title;
    this.desc = desc;
    this.lat = lat;
    this.lon = lon;
    this.pub = pub;
    this.p = p;
    this.n = n;
}

public item(Parcel in) {
    // TODO Auto-generated constructor stub
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub

}

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

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

}

(我删除了所有的getter和setter以减少代码)

1 个答案:

答案 0 :(得分:1)

the docs page on Parcelable。与Serializable不同,仅使用界面将您的班级标记为Parcelable是不够的。你还需要做更多的事情:

  1. 您需要实现writeToParcel方法以及将Parcel对象作为唯一参数的构造函数。请注意这两个是如何在该页面上的示例代码中构建的;请注意,您在writeToParcel中将数据写入Parcel的顺序与您在该构造函数中读取它的顺序相同。
  2. 您需要在Item类中添加以下内容:
  3. public static final Parcelable.Creator<Item> CREATOR 
            = new Parcelable.Creator<Item>() {
        public Item createFromParcel(Parcel in) {
            return new Item(in);
        }
    
        public Item[] newArray(int size) {
            return new Item[size];
        }
    };