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

时间:2017-07-17 15:25:40

标签: java android object android-intent

我创建了一个对象,其中包含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);
    }
}

当我开始将对象从MainActivity发送到SecondActivity时,会发生错误,我的应用程序停止运行。

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

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

startActivity(intent);

我认为它与 MemoryCard 构造函数的参数 this R.drawable.myImage 有关,但为什么呢?

1 个答案:

答案 0 :(得分:1)

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