mercredi 18 janvier 2023

Java random character generated then output in the text attribute of a TextView element in Android Studio

I am trying to get a random character generated from a String array and then set it as the text for a TextView element. Currently the IDE does not show any syntax or logic errors but the app crashes when run on my phone.

I have set the random generation to take place when the Start button is pressed.

public void startGame (View starGame) { //ISOLATE RANDOM FUNCTION TO SEE IF IT WORKED OR NOT
    String notesLevelOne = "ABCDEF";
    Random rnd = new Random();
    char note = notesLevelOne.charAt(rnd.nextInt(notesLevelOne.length()));
    TextView noteQuestion = findViewById(R.id.noteLetterTxt); //capture the textView "welcomeMessage" in Welcome layout
    noteQuestion.setText(note); //set message string as textView text
}

This is the code in the main activity and the button has the onClick set to startGame.




Aucun commentaire:

Enregistrer un commentaire