Search code examples
androidandroid-intentserializableandroid-bundle

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();

    }
}

Solution

  • here Path, 'RectF', Matrix, are not my classes, they are android classes, and i can't implement Serializable in these classes..

    Then do not have fields for them in Serializable classes.

    Either:

    • Do not have separate activities here, but do something else instead (e.g., one activity and two fragments) that avoids the need for the Intent, or

    • Do not pass this data between the activities, but instead use a different application architecture (e.g., the data is not held by either activity, but instead is held by a repository that both activities can talk to), or

    • Create some data structure that you can make Parceable or Serializable, from which you can rebuild your model objects with their desired classes, and pass that Parcelable/Serializable data structure in the Intent instead of TData