简体   繁体   English

无法通过意图将自制的可序列化对象传递给另一个活动

[英]Cannot pass selfmade serializable object to another activity via intent

I created an object which contains data fields of the types ImageView, boolean and Integer. 我创建了一个对象,其中包含ImageView,boolean和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. 当我开始将对象从MainActivity发送到SecondActivity时,会发生错误,并且我的应用程序停止运行。

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? 我认为这与MemoryCard构造函数的thisR.drawable.myImage参数有关 ,但是为什么呢?

Is ImageView serializable? ImageView是否可序列化? An object if serializable only if it implements Serializable and all its members are serializable. 仅当对象实现Serializable 并且其所有成员都可序列化时,才可Serializable化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM