Reputation:
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:
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<Question> qsts = new ArrayList<Question>();
List<Integer> generated = new ArrayList<Integer>();
ArrayList<String> allAnswers = new ArrayList<String>();
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;
}
}
}
I was creating the objects in the Question class code and was working with static
objects, but following the tips, I made that way.
Here is the class 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;
}
I know the problem is in the comparation of the if
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 nextQuestion.correctAnswerText
. Am I declaring something in the wrong place?
Observation: the app stops when I click one of the 4 buttons that contains the answer (correct or wrong)
Upvotes: 0
Views: 2661
Reputation: 2528
See your are declaring Question nextQuestion
twice.
One Globally and another one in function generateQuestion()
And the global nextQuestion
is null and that is your error.
Replace this(Question nextQuestion = qsts.get(nxt);
) line in function generateQuestion()
with nextQuestion = qsts.get(nxt);
Solution for your comment.
allAnswers.clear()
do this before generating new question.
if(buttonText.equals(nextQuestion.correctAnswerText))
{
textView2.setText("CORRECT!");
allAnswers.clear();
generateQuestion();
return;
}else{
textView2.setText("WRONG!");
allAnswers.clear();
generateQuestion();
return;
}
Upvotes: 1