0

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Ramón Wilhelm
  • 967
  • 2
  • 18
  • 39
  • 3
    Why not just sending Class object which hold all required data in next Activity to show `ImageView` instead of sending `ImageView` object ? – ρяσѕρєя K Jul 17 '17 at 15:27
  • 1
    "my App stops running" -- use LogCat to examine the Java stack trace associated with your crash": https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this As others have pointed out, most likely, you are crashing on the `ImageView`, as that cannot be serialized. `SecondActivity` has no means of modifying the UI of `MainActivity` directly, which is my guess as to what you are trying to accomplish here. – CommonsWare Jul 17 '17 at 15:34

1 Answers1

1

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

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257