我试图在活动之间传递一个对象,但我的应用程序崩溃了:
这是我的课程(我通过意图的对象):
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.putExtra("thing",markers.get(i));
startActivity(Intent);
我在目的地活动中得到了额外的东西:
item item = getIntent().getParcelableExtra("thing");
我的日志猫:
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 以减少代码)