无法通过意图将自制的可序列化对象传递给另一个活动 [英] Cannot pass selfmade serializable object to another activity via intent

查看:41
本文介绍了无法通过意图将自制的可序列化对象传递给另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个对象,其中包含ImageView,boolean和Integer类型的数据字段.

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

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

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

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

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

推荐答案

ImageView 是否可序列化?对象只有在实现了 Serializable 并且其所有成员都是可序列化的时才可序列化.

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

这篇关于无法通过意图将自制的可序列化对象传递给另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆