首页 > 解决方案 > java.lang.RuntimeException: Parcelable encountered IOException writing serializable

问题描述

I have an arraylist that i want to send from 1 activity to another, I am using serializable but end up getting following error message.

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.app...)

I have also reviewed some question regarding this on SO, most of them saying that all classes defined inside your Serialized class should also implement Serializable, but here Path, 'RectF', Matrix, are not my classes, they are android classes, and i can't implement Serializable in these classes.

This is how i am sending array list from 1 activity to another.

Intent intent= new Intent(MainActivity.this, DetailsActivity.class);
        Bundle bundle= new Bundle();
        bundle.putSerializable("PATH_LIST", pathsList);
        bundle.putString("FILE_NAME", fileName);
        intent.putExtras(bundle);
        startActivity(intent);

and this is my class.

public class TData implements Serializable {

    Matrix originalMatrix;
    public Path path;
    PointF position;



    private TData attachedPathData;
    public void setAttachedPathData(TData pathData){
        attachedPathData = pathData;
    }
    public TData getAttachedPathData(){
        return attachedPathData;
    }


    public TData(){

    }


    public TData(Path path, PointF position, String id, String fillColor, String strokeColor){
        this.path = path;
        this.position = position;
        this.id = id;

        this.fillColor = fillColor;
        this.strokeColor = strokeColor;
    }

    public void Scale(float scaleX, float scaleY){
        this.scaleX = scaleX;
        this.scaleY = scaleY;

        Matrix scaleMatrix = new Matrix();
        RectF rectF = new RectF();
        path.computeBounds(rectF, true);
        scaleMatrix.setScale(scaleX,scaleY);
        path.transform(scaleMatrix);


        Matrix mat = new Matrix();
        path.computeBounds(rf, true);

        Region r = new Region();

    }
}

标签: androidandroid-intentserializableandroid-bundle

解决方案


这里的 Path,'RectF',Matrix,不是我的类,它们是 android 类,我无法在这些类中实现 Serializable..

然后在类中没有他们的字段Serializable

任何一个:

  • 这里没有单独的活动,而是做一些其他事情(例如,一个活动和两个片段),以避免需要Intent, 或

  • 不要在活动之间传递这些数据,而是使用不同的应用程序架构(例如,数据不由任何一个活动保存,而是由两个活动可以与之通信的存储库保存),或者

  • 创建一些您可以制作的数据结构Parceableor ,您可以从中使用Serializable所需的类重建模型对象,并将该Parcelable/Serializable数据结构传递给IntentTData


推荐阅读