Je débute dans la programmation mobile et dans le java et j'aurais donc besoin de votre aide.
Je fais une application android qui quand on appuis sur un bouton cela affiche une image et un texte associé de manière aléatoire.
Voici le code de mon activité :
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
// Nombre aléatoire
ImageView imgView = new ImageView(this);
Random rand = new Random();
int rndInt = rand.nextInt(84) + 1; //n = the number of images, that start at idx 1
String imgName = "img" + rndInt;
// ImageView
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setImageResource(id);
// TextView
// Bouton
final Button button = findViewById(R.id.nouvelle_position);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
Intent main2 = new Intent(MainActivity2.this , MainActivity2.class);
startActivity(main2);
}
});
}
}
Mais quand je lances l'application sur mon smartphone aucun texte ou image apparaît à l'écran.
Est-ce que quelqu'un pourrait m'orienter sur ce que j'ai oublié ou mal fait ?
Merci
If you like programation :Then :call me for chat: Else: call me for that I explain to you why it's sooo coool
Android : afficher une ImageView aléatoirement
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.