0

目前我有问题将ArrayList<MyClass>一个活动从一个活动传递到另一个活动。我试图在 MyClass 中实现 Parcelable 并在活动中使用意图,但似乎我做错了什么。当我打电话startActivityintent应用程序的窗口冻结并收到错误“!!! Bender Transaction 失败!!!”。我在这里和其他一些地方读过交易的大小可能会导致错误。MyClass 拥有一个图像。目前startActivity称为ArrayList保存五个元素,每个元素有一个位图。所有图像的大小加在一起小于 70kb。

这是我的实际代码:

public class MyClass implements Parcelable {

public enum Type {
    VIDEO, AUDIO
}

private int id;
private String name;
private String url;
private double longitude;
private double latitude;
private double gpsDistance;
private String description;
private String thumb;
private Bitmap thumbpic;
private Type type;

public MyClass() {
    this.gpsDistance = 0;
}

public MyClass(Parcel in) {
    readFromParcel(in);
}

/* Setter & Getter */


public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(id);
    dest.writeString(name);
    dest.writeString(url);
    dest.writeDouble(longitude);
    dest.writeDouble(latitude);
    dest.writeDouble(gpsDistance);
    dest.writeString(description);
    dest.writeString(thumb);
    dest.writeParcelable(thumbpic, flags);
    dest.writeString((type == null) ? "" : type.name());

}



private void readFromParcel(Parcel in) {
    this.id = in.readInt();
    this.name = in.readString();
    this.url = in.readString();
    this.longitude = in.readDouble();
    this.latitude = in.readDouble();
    this.gpsDistance = in.readDouble();
    this.description = in.readString();
    this.thumb = in.readString();
    this.thumbpic = (Bitmap) in.readParcelable(getClass().getClassLoader());
    try {
        type = Type.valueOf(in.readString());
    } catch (IllegalArgumentException x) {
        type = null;
    }
}

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

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

活动 ( ) 中的一些代码片段MainActivity开始第二个 ( ListActivity)

ArrayList<Content> contentElements  = new ArrayList<Content>();

Intent intent = new Intent(MainActivity.this,ListActivity.class);

intent.putParcelableArrayListExtra("content", contentElements);

中的处理ListActivity

Intent i = getIntent();
contentElements= i.getParcelableArrayListExtra("content");

有人知道导致此问题的原因并有解决方案或替代方法吗?

非常感谢!

4

0 回答 0