Rhohen Rhohen - 1 month ago 7
Android Question

setImageResource causing crash of application

i'm trying to make a button that when clicked on it picks a random card from a set of card and show it into an ImageView.
To do this I have a Card class for their value and their id (the image card name in drawable).

public class Carte {

int valeur;
static String nomCarte;

public Carte(int valeur, String nomImage) {

valeur = this.valeur;
nomCarte = this.nomCarte;
}

public static String getImageId() {
return nomCarte;
}
}


I initialize 52 of them into a tab[], when the button draw is clicked it pick a random one from the tab[] and i try to then change my ImageView with the image of the card picked by getting the id of the card picked and using an setImageResource but it make my application crash can you help me there ?

Here's my mainActivity code :

public class MainActivity extends AppCompatActivity {

protected Carte tab[];
protected ImageView imageCarte;
protected Button afficherCarte;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageCarte = (ImageView) findViewById(R.id.imageCarte);

tab = new Carte[]{new Carte(1, "c_1"), new Carte(1, "d_1"), new Carte(1, "h_1"), new Carte(1, "s_1"),
new Carte(2, "c_2"), new Carte(2, "d_2"), new Carte(2, "h_2"), new Carte(2, "s_2"),
new Carte(3, "c_2"), new Carte(3, "d_3"), new Carte(3, "h_3"), new Carte(3, "s_3"),
new Carte(4, "c_4"), new Carte(4, "d_4"), new Carte(4, "h_4"), new Carte(4, "s_4"),
new Carte(5, "c_5"), new Carte(5, "d_5"), new Carte(5, "h_5"), new Carte(5, "s_5"),
new Carte(6, "c_6"), new Carte(6, "d_6"), new Carte(6, "h_6"), new Carte(6, "s_6"),
new Carte(7, "c_7"), new Carte(7, "d_7"), new Carte(7, "h_7"), new Carte(7, "s_7"),
new Carte(8, "c_8"), new Carte(8, "d_8"), new Carte(8, "h_8"), new Carte(8, "s_8"),
new Carte(9, "c_9"), new Carte(9, "d_9"), new Carte(9, "h_9"), new Carte(9, "s_9"),
new Carte(10, "c_10"), new Carte(10, "d_10"), new Carte(10, "h_10"), new Carte(10, "s_10"),
new Carte(11, "c_11"), new Carte(11, "d_11"), new Carte(11, "h_11"), new Carte(11, "s_11"),
new Carte(12, "c_12"), new Carte(12, "d_12"), new Carte(12, "h_12"), new Carte(12, "s_12"),
new Carte(13, "c_13"), new Carte(13, "d_13"), new Carte(13, "h_13"), new Carte(13, "s_13"),};


afficherCarte = (Button) findViewById(R.id.afficherCarte);
afficherCarte.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Random r = new Random();
int i1 = r.nextInt(53 - 1) + 1;
int id = getResources().getIdentifier(tab[i1].getImageId(), "drawable", getPackageName());
imageCarte.setImageResource(id);
}
});

}

}

Answer

Your constructor is wrong and you made a static field on your imageId which will always be the same id if you don't remove it.

 public Carte(int valeur, String nomImage) {
    valeur = this.valeur;
    nomCarte = this.nomCarte;
 }

You are never storing the method parameter to the class fields. You have to use it the other way around like

this.valeur = valeur;
this.nomCarte = nomCarte;

And your getter does return null because of that. Besides that, remove the static modifier

 public String getImageId() {
    return nomCarte;
 }
Comments