Cannot pass selfmade serializable object to another activity via intent

吃可爱长大的小学妹 提交于 2020-01-06 05:28:29

问题


I created an object which contains data fields of the types ImageView, boolean and Integer.

public class MemoryCard implements View.OnClickListener, Serializable {

    private ImageView image;
    private int id;
    private int nr;
    private boolean clicked = false;
    private int cardBack;
    private int cardFront;

    public MemoryCard(Context context, int id, int nr, int cardFront)
    {
        this.id = id;
        this.nr = nr;
        this.cardBack = R.drawable.backside;
        this.cardFront = cardFront;
        this.image = new ImageView(context);
        this.image.setImageResource(this.cardBack);
        this.image.setOnClickListener(this);
    }
}

When I start to send an object from MainActivity to SecondActivity then an error occurs and my App stops running.

Intent intent = new Intent(MainActivity.this, SecondActivity.class);

intent.putExtra("object", new MemoryCard(this, 0, 0, R.drawable.myImage));

startActivity(intent);

I think it has something to do with the parameters this and R.drawable.myImage of the MemoryCard constructor but why?


回答1:


Is ImageView serializable? An object if serializable only if it implements Serializable and all its members are serializable.



来源:https://stackoverflow.com/questions/45148045/cannot-pass-selfmade-serializable-object-to-another-activity-via-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!