Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>onClick</code> provides you with the view that was clicked (<code>arg0</code> in your code), so pass that to your <code>checkAnswer</code> method. Then in <code>checkAnswer</code> (or <code>getSelectedAnswer</code>) you can simply check which view was clicked and use it directly to validate the answer.</p> <p>The method you use now is probably causing some kind of recursion because you invoke <code>onClick</code> for each button in <code>getSelectedAnswer</code> and that results in a stack overflow.</p> <hr> <p><strong>Edit:</strong> example code (untested) to illustrate the idea</p> <pre><code>@Override public void onClick(View arg0) { // NEW: pass the received view to checkAnswer if(!checkAnswer(arg0)) return; if (currentGame.isGameOver()){ //Log.d("Questions", "End of game! lets add up the scores.."); //Log.d("Questions", "Questions Correct: " + currentGame.getRight()); //Log.d("Questions", "Questions Wrong: " + currentGame.getWrong()); Intent i = new Intent(this, EndgameActivity.class); startActivity(i); finish(); } else{ Intent i = new Intent(this, QuestionActivity.class); startActivity(i); finish(); } } // NEW: add view as argument to checkAnswer private boolean checkAnswer(View v) { // NEW: retrieve answer from clicked view Button b = (Button)v; String answer = b.getText().toString(); // now check if the answer is correct if (currentQ.getAnswer().equalsIgnoreCase(answer)) { //Log.d("Questions", "Correct Answer!"); currentGame.incrementScore(); } else { //Log.d("Questions", "Incorrect Answer!"); currentGame.decrementScore(); } return true; } </code></pre> <p>Since I don't exactly know how your app is laid out, I am assuming here that you have four "buttons", each with an answer set as it's caption. What the code above does is retrieve the view that was clicked and extract it's caption (text), then checks that against the correct answer. Of course since you could also <strong>NOT</strong> click one of the buttons you will probably need to check if the clicked view is indeed one of the buttons, and if not you should skip the answer checking - I have omitted this in the example code above.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload