ImagesPath.java
public class ImagesPath {
String path;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public ImagesPath(){}
public ImagesPath(String path){
this.path=path;
}
}
PathList.java实现Parcelable接口
public class PathList extends ArrayList<ImagesPath> implements Parcelable{
public PathList(){}
public PathList(Parcel in){
readFromParcel(in);
}
@SuppressWarnings("unchecked")
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public PathList createFromParcel(Parcel in) {
return new PathList(in);
}
public Object[] newArray(int arg0) {
return null;
}
};
private void readFromParcel(Parcel in) {
this.clear();
//First we have to read the list size
int size = in.readInt();
//Reading remember that we wrote first the Name and later the Phone Number.
//Order is fundamental
for (int i = 0; i < size; i++) {
ImagesPath c = new ImagesPath();
c.setPath(in.readString());
this.add(c);
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
int size = this.size();
//We have to write the list size, we need him recreating the list
dest.writeInt(size);
//We decided arbitrarily to write first the Name and later the Phone Number.
for (int i = 0; i < size; i++) {
ImagesPath c = this.get(i);
dest.writeString(c.getPath());
}
}
}
MainActivity用于在Arraylist中添加值
PathList pathList;
ImagesPath c1;
c1 = new ImagesPath(mCurrentPhotoPath);
pathList.add(c1);
Bundle b = new Bundle();
Intent i = new Intent(CameraActivity.this, CameraActivity.class);
b.putParcelable("list", c1); // Getting Error over here 2nd Argument should be parcelable required parcealbe and found ImagesPath
i.putExtras(b);
startActivity(i);
使用putParceable()方法以捆绑方式传递ImagesPath对象时出现错误,发现ImagesPath必需为Parcelable
最佳答案
您收到此错误的原因是您没有在类型Parcelable
中实现ImagesPath
。
因此,当为ImagesPath
实现它时,就无需为ArraList<ImagesPath>
实现它,而可以使用Bundle
的putParcelableArraList方法
public class ImagesPath
{
String path;
@Override
public int describeContents()
{
return 0;
}
public ImagesPath (Parcel in)
{
readFromParcel(in);
}
@SuppressWarnings("unchecked")
public static final Parcelable.Creator CREATOR = new Parcelable.Creator()
{
public ImagesPath createFromParcel(Parcel in)
{
return new ImagesPath(in);
}
public Object[] newArray(int arg0)
{
return null;
}
};
private void readFromParcel(Parcel in)
{
String path; = in.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags)
{
dest.writeString(path);
}
}