title: Android: Can't identify the correct answer in a quiz game
body: <p>Well, I'm with this problem about 3 days. I'm making a quiz game, but I can't identify if the clicked button contains the correct answer or not. Following some tips that I saw around here, I did some alterations in my code. See the new code:</p>
  
  <pre><code>package com.app;
  
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.List;
  import java.util.Random;
  
  import android.os.Bundle;
  import android.app.Activity;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.TextView;
  
  public class MyActivity extends Activity implements OnClickListener {
  
  
      TextView textView1, textView2;
      Button btn1, btn2, btn3, btn4;
  
      ArrayList&lt;Question&gt; qsts = new ArrayList&lt;Question&gt;();
      List&lt;Integer&gt; generated = new ArrayList&lt;Integer&gt;();
  
      ArrayList&lt;String&gt; allAnswers = new ArrayList&lt;String&gt;();
  
      Random rng = new Random();
      Question nextQuestion;
  
  
  // Creating the objects Question that recieves the values "questionText", "correctAnswerText", and the other 3 wrong answers "wrongAnswer1" 2 and 3...
  
      Question q1 = new Question(
              "Question 1",
  
              "Correct Answer - Question 1",
              "Wrong 1 - Question 1",
              "Wrong 2 - Question 1",
              "Wrong 3 - Question 1"
              );
      Question q2 = new Question(
              "Question 2",
  
              "Correct Answer - Question 2",
              "Wrong 1 - Question 2",
              "Wrong 2 - Question 2",
              "Wrong 3 - Question 2"
              );
      Question q3 = new Question(
              "Question 3",
  
              "Correct Answer - Question 3",
              "Wrong 1 - Question 3",
              "Wrong 2 - Question 3",
              "Wrong 3 - Question 3"
              );
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_other);
  
  
      // ADD THE QUESTIONS IN THE ArrayList qsts
  
      qsts.add(q1);           
          qsts.add(q2);
          qsts.add(q3);
  
          textView1 = (TextView) findViewById(R.id.textView1);
              textView2 = (TextView) findViewById(R.id.textView2);
  
              btn1 = (Button) findViewById(R.id.btn1);
              btn2 = (Button) findViewById(R.id.btn2);
              btn3 = (Button) findViewById(R.id.btn3);
              btn4 = (Button) findViewById(R.id.btn4);
  
              btn1.setOnClickListener(this);
              btn2.setOnClickListener(this);
              btn3.setOnClickListener(this);
              btn4.setOnClickListener(this);
  
          generateQuestion();
  
      }
  
          public void generateQuestion(){
  
  
  
  
              while(true){
  
                  int nxt = rng.nextInt(3);
  
                  if (!generated.contains(nxt)){
  
                      generated.add(nxt);
  
                      Question nextQuestion = qsts.get(nxt);
  
                      textView1.setText(nextQuestion.questionText);
  
                      allAnswers.add(nextQuestion.correctAnswerText);
                      allAnswers.add(nextQuestion.wrongAnswer1);
                      allAnswers.add(nextQuestion.wrongAnswer2);
                      allAnswers.add(nextQuestion.wrongAnswer3);
  
                      Collections.shuffle(allAnswers);
  
                      btn1.setText(allAnswers.get(0));
                      btn2.setText(allAnswers.get(1));
                      btn3.setText(allAnswers.get(2));
                      btn4.setText(allAnswers.get(3));
  
                      break;
                  }
              }
          }
  
          @Override
          public void onClick(View v) {
              Button b = (Button)v;
              String buttonText = b.getText().toString();
  
              if(buttonText.equals(nextQuestion.correctAnswerText))
              { 
  
                  textView2.setText("CORRECT!");
                  generateQuestion();
                  return;
  
              }else{
                  textView2.setText("WRONG!");
                  generateQuestion();
                  return;
              }
  
          }
  }
  </code></pre>
  
  <p>I was creating the objects in the Question class code and was working with <code>static</code> objects, but following the tips, I made that way.</p>
  
  <p>Here is the class code:</p>
  
  <pre><code>package com.app;
  
  public class Question {
  
      String questionText;
      String correctAnswerText;       
      String wrongAnswer1;
      String wrongAnswer2;
      String wrongAnswer3;
  
  
      Question (String qst, String cAns, String wAns1, String wAns2, String wAns3){
  
          questionText = qst;
          correctAnswerText = cAns;
          wrongAnswer1 = wAns1;
          wrongAnswer2 = wAns2;
          wrongAnswer3 = wAns3;
  
      }
  </code></pre>
  
  <p>I know the problem is in the comparation of the <code>if</code> in the onClick method, 'cause when I compare the String buttonText with something else, it works. Please, someone tell me why I can't compare with the <code>nextQuestion.correctAnswerText</code>. Am I declaring something in the wrong place?</p>
  
  <p>Observation: the app stops when I click one of the 4 buttons that contains the answer (correct or wrong)</p>