-2

I have written some simple code to generate a random value in my array. It generates that random value, but I cannot set my textView equal to that value. This is a very simple problem, but I can't seem to find the solution anywhere.

Here is my code...

final String[] group_topics = {"Walking", "Swimming", "Running"};

public void getRandomTopic1() {
    Random random = new Random();

    int index = random.nextInt(group_topics.length);

    topicOne.setText(group_topics[index]);



    topicOne = (TextView) findViewById(R.id.textView2);
}
0xOsiris
  • 247
  • 2
  • 17

2 Answers2

3
topicOne = (TextView) findViewById(R.id.textView2);
 topicOne.setText(group_topics[index]);

At First You will have to wire the widget and then acess it . you doing it the other way which will not work. try this and it will work.

Sumanth Jois
  • 3,146
  • 4
  • 27
  • 42
0

You did not instantiate your TextView before setting a value to it. I have changed the code slightly.

final String[] group_topics = {"Walking", "Swimming", "Running"};

public void getRandomTopic1() {
    Random random = new Random();

    int index = random.nextInt(group_topics.length);

    topicOne = (TextView) findViewById(R.id.textView2);

    topicOne.setText(group_topics[index]);

}

Inducesmile
  • 2,475
  • 1
  • 17
  • 19