Android在活动之间传递parcelable对象列表

时间:2016-04-12 06:57:35

标签: android parameter-passing parcelable

我的问题是在我将列表传递给第二个活动后,我无法将其转发回列表。它崩溃的app.I试过try catch但仍然没有例外。知道我做错了什么吗? Tyvm。

活动1

 private ArrayList<ResimBean> rbList = new ArrayList<ResimBean>();

 Intent intent = new Intent(getApplicationContext(), ResimListActivity.class);
 intent.putParcelableArrayListExtra("reslist",rbList);
                    startActivityForResult(intent, 100);

活动2

 private ArrayList<ResimBean> rbList = new ArrayList<ResimBean>();


if (extras != null) {
            try {

                Intent i = getIntent();
                rbList = (ArrayList<ResimBean>)i.getParcelableArrayListExtra("reslist");
            } catch (Exception ex) {
                String msg = ex.getMessage();
            }

        }

和我的班级

public class ResimBean implements Parcelable {

    private int Id;
    private int HataBildirimId;
    private byte[] Resim;
    private byte[] Thumbnail;

    public byte[] getThumbnail() {
        return Thumbnail;
    }

    public void setThumbnail(byte[] thumbnail) {
        Thumbnail = thumbnail;
    }

    private String Path;

    public String getPath() {
        return Path;
    }

    public void setPath(String path) {
        Path = path;
    }

    public int getHataBildirimId() {
        return HataBildirimId;
    }

    public void setHataBildirimId(int hataBildirimId) {
        HataBildirimId = hataBildirimId;
    }

    public int getId() {
        return Id;
    }

    public void setId(int id) {
        Id = id;
    }

    public byte[] getResim() {
        return Resim;
    }

    public void setResim(byte[] resim) {
        Resim = resim;
    }
 @Override
    public int describeContents() {
        return 0;
    }
  @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(HataBildirimId);
        dest.writeByteArray(Resim);
        dest.writeByteArray(Thumbnail);

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

    public void readFromParcel(Parcel in){
        this.HataBildirimId = in.readInt();
        this.Resim = new byte[in.readInt()];
        this.Thumbnail = new byte[in.readInt()];
    }

    public static final Parcelable.Creator<ResimBean> CREATOR = new Parcelable.Creator<ResimBean>() {
        @Override
        public ResimBean createFromParcel(Parcel in) {
            return new ResimBean(in);
        }

        @Override
        public ResimBean[] newArray(int size) {
            return new ResimBean[size];
        }
    };
}

修改 我改为rbList = i.getParcelableArrayListExtra("reslist");,但它仍然崩溃。

编辑2 这是我的logcat错误。

04-12 10:27:34.786: E/Surface(9374): getSlotFromBufferLocked: unknown buffer: 0xa5d110d0

编辑3 这里我如何填充列表

   Bitmap bmp = (Bitmap) data.getExtras().get("data");
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] thumbArray = stream.toByteArray();

            Uri tempUri = getImageUri(getApplicationContext(), bmp);
            String picPath = getRealPathFromURI(tempUri);
            Bitmap yourSelectedImage = BitmapFactory.decodeFile(picPath);
            byte[] bar = getBytesFromBitmap(yourSelectedImage);

            ResimBean rb = new ResimBean(Parcel.obtain());

            rb.setResim(bar);
            rb.setThumbnail(thumbArray);
            rb.setPath(picPath);
            rbList.add(rb);

4 个答案:

答案 0 :(得分:0)

 rbList = (ArrayList<ResimBean>)i.getSerializableExtra("reslist");

这是您传递parcelable并获取可序列化的代码中的问题。

答案 1 :(得分:0)

您的传递时间为putParcelableArrayListExtra,访问时间为getSerializableExtra Extra,这是错误的

替换

rbList =(ArrayList)i.getSerializableExtra(&#34; reslist&#34;);

rbList = i.getParcelableArrayListExtra(&#34; reslist&#34;);

答案 2 :(得分:0)

基于添加的错误日志(E/Surface(9374): getSlotFromBufferLocked: unknown buffer: 0xa5d110d0):

这是Marshmallow(6.0)中的known issue。它已在6.0.1中修复

答案 3 :(得分:0)

您将数据从Activity1传递到Activity2。为什么?你使用startActivityForResult(intent,100);

试试这个。

Intent intent = new Intent(getApplicationContext(), ResimListActivity.class);
intent.putParcelableArrayListExtra("reslist",rbList);
startActivity(intent);