Java 使用Parcelable和Intent在两个活动之间传递ArrayList
目前,我在将Java 使用Parcelable和Intent在两个活动之间传递ArrayList,java,android,android-intent,arraylist,parcelable,Java,Android,Android Intent,Arraylist,Parcelable,目前,我在将ArrayList从一个活动传递到另一个活动时遇到了问题。我试图在MyClass中实现Parcelable,并在活动中使用意图,但似乎我做错了什么。当我用intent调用startActivity时,应用程序的窗口冻结,我得到错误“!!!Bender事务失败!!!”。我在这里和其他一些地方读到,事务的大小可能会导致错误。MyClass保存一个图像。此时,startActivity被称为ArrayList包含五个元素,每个元素有一个位图。所有图像的大小加在一起小于70kb 这是我的实际
ArrayList
从一个活动传递到另一个活动时遇到了问题。我试图在MyClass中实现Parcelable,并在活动中使用意图,但似乎我做错了什么。当我用intent
调用startActivity
时,应用程序的窗口冻结,我得到错误“!!!Bender事务失败!!!”。我在这里和其他一些地方读到,事务的大小可能会导致错误。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];
}
};
}
启动第二个活动(ListActivity
)的活动(MainActivity
)中的一些代码片段
是否有人知道是什么导致了这个问题,并有解决方案或替代方法
非常感谢 这个问题可能重复的问题现在已经解决了。位图的大小太大,无法使用intent。为了处理它,我将它们保存到FileDir中,MyClass保存URI而不是真正的位图。
ArrayList<Content> contentElements = new ArrayList<Content>();
Intent intent = new Intent(MainActivity.this,ListActivity.class);
intent.putParcelableArrayListExtra("content", contentElements);
Intent i = getIntent();
contentElements= i.getParcelableArrayListExtra("content");