传递对象时,启动活动后粉碎getParcelableExtra() [英] getParcelableExtra() crushing after Starting activity when passing an object

查看:78
本文介绍了传递对象时,启动活动后粉碎getParcelableExtra()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在活动之间传递对象,但是我的应用程序崩溃了:

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");

我的日志:

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方法)

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

推荐答案

请参见 Parcelable上的文档页面。与可序列化不同,仅通过界面将您的班级标记为 Parcelable 是不够的。您还需要做一些其他事情:

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. 您需要实现 writeToParcel 方法以及将 Parcel 对象作为其唯一参数的构造函数。请注意该页面上的示例代码中这两个的构造方式。请注意,在 writeToParcel 中将数据写入包裹的顺序与在该构造函数中读取数据的顺序相同。

  2. 您需要在Item类中添加以下内容:

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

这篇关于传递对象时,启动活动后粉碎getParcelableExtra()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆