簡體   English   中英

無法通過意圖將自制的可序列化對象傳遞給另一個活動

[英]Cannot pass selfmade serializable object to another activity via 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構造函數的thisR.drawable.myImage參數有關 ,但是為什么呢?

ImageView是否可序列化? 僅當對象實現Serializable 並且其所有成員都可序列化時,才可Serializable化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM