getParcelableExtra() crushing after Starting activity when passing an object

家住魔仙堡 提交于 2019-12-13 04:29:39

问题


Im trying to pass an object between activities but my app crushes:

This is my class (the object im passing through the intent):

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

I call the intent with an object (the object is within a list):

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

And im getting the Extra at the destination activity:

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

My 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

Edit:

My whole class:

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];
}
};

}

(I removed all the getters and setters for less code)


回答1:


See the docs page on Parcelable. Unlike Serializable, it's not enough to just mark your class as Parcelable with the interface. You have to do a few more things:

  1. You need to implement the writeToParcel method as well as a constructor that takes in a Parcel object as its only argument. Note how these two are constructed in the sample code on that page; be careful that the order you write data to the Parcel in writeToParcel is the same order you read them out in that constructor.
  2. You need to add the following inside your Item class:

 

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];
    }
};


来源:https://stackoverflow.com/questions/18166205/getparcelableextra-crushing-after-starting-activity-when-passing-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!