I am creating this activity where using import java.util.Random;
, it fetches a question from the string-array inside the arrays.xml
. The item fetches from the array is displayed in a textview at the top of my layout. What I want to achieve here is having a matching background from every item fetches from the string-array. My approach to this was to match the generated index fetched from the questions array and create an if statement.If item=0, then this background shown in * in the code. This is what I implemented so far, open to any suggestions or improvements to my code:
Activity
questions = getResources().getStringArray(R.array.questions);
@SuppressLint("NewApi") private void getQuestion(){
TextView questionText = (TextView) findViewById(R.id.question);
Random rand = new Random();
int maxIndex = questions.length;
int generatedIndex = rand.nextInt(maxIndex);
if(generatedIndex == 1){
**screenGame.setBackground(getResources().getDrawable(R.drawable.footballfindtheposition));
}
else
screenGame.setBackground(getResources().getDrawable(R.drawable.mainbg));
questionText.setText(questions[generatedIndex]);
}**
arrays.xml
<string-array name="questions">
<item>Click on the player with the possition of a Center Back</item>
<item>Click on the Player with the possition of Left Center Midfield</item>
<item>Click on the blabla</item>
</string-array>
I get a NullPointerException at the end of my code in LogCat