单击按钮时如何检查我的答案是错误还是正确?

时间:2013-09-02 10:00:24

标签: android database button

我正在进行Android测验。我有4个按钮和逻辑,以便在点击任何按钮时检查我的答案是对还是错。但它不起作用。

public class QuestionActivity extends Activity implements OnClickListener{

    private Question currentQ;
    private GamePlay currentGame;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question);
        /**
         * Configure current game and get question
         */
        currentGame = ((CYKApplication)getApplication()).getCurrentGame();
        currentQ = currentGame.getNextQuestion();
        Button nextBtn1 = (Button) findViewById(R.id.answer1);
        nextBtn1.setOnClickListener(this);
        Button nextBtn2 = (Button) findViewById(R.id.answer2);
        nextBtn2.setOnClickListener(this);
        Button nextBtn3 = (Button) findViewById(R.id.answer3);
        nextBtn3.setOnClickListener(this);
        Button nextBtn4 = (Button) findViewById(R.id.answer4);
        nextBtn4.setOnClickListener(this);
        /**
         * Update the question and answer options..
         */
        setQuestions();

    }


    /**
     * Method to set the text for the question and answers from the current games
     * current question
     */
    private void setQuestions() {
        //set the question text from current question
        String question = Utility.capitalise(currentQ.getQuestion());
        TextView qText = (TextView) findViewById(R.id.question);
        qText.setText(question);

        //set the available options
        List<String> answers = currentQ.getQuestionOptions();
        TextView option1 = (TextView) findViewById(R.id.answer1);
        option1.setText(Utility.capitalise(answers.get(0)));

        TextView option2 = (TextView) findViewById(R.id.answer2);
        option2.setText(Utility.capitalise(answers.get(1)));

        TextView option3 = (TextView) findViewById(R.id.answer3);
        option3.setText(Utility.capitalise(answers.get(2)));

        TextView option4 = (TextView) findViewById(R.id.answer4);
        option4.setText(Utility.capitalise(answers.get(3)));
    }


    @Override
    public void onClick(View arg0) {
        //Log.d("Questions", "Moving to next question");

    if(!checkAnswer()) return;
        /**
         * check if end of game
         */
        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();
        }
    }




    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        switch (keyCode)
        {
        case KeyEvent.KEYCODE_BACK :
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }


    /*
      Check if a checkbox has been selected, and if it
      has then check if its correct and update gamescore
     */
    private boolean checkAnswer() {
        String answer = getSelectedAnswer();
        if (answer==null){
            //Log.d("Questions", "No Checkbox selection made - returning");
            return false;
        }
        else {
            //Log.d("Questions", "Valid Checkbox selection made - check if correct");
            if (currentQ.getAnswer().equalsIgnoreCase(answer))
            {
                //Log.d("Questions", "Correct Answer!");
                currentGame.incrementScore();
            }
            else{
                //Log.d("Questions", "Incorrect Answer!");
                currentGame.decrementScore();
            }
            return true;
        }
    }



    private String getSelectedAnswer() {
        Button c1 = (Button)findViewById(R.id.answer1);
        Button c2 = (Button)findViewById(R.id.answer2);
        Button c3 = (Button)findViewById(R.id.answer3);
        Button c4 = (Button)findViewById(R.id.answer4);
        if (c1.callOnClick())
        {
            return c1.getContext().toString();
        }
        if (c2.callOnClick())
        {
            return c2.getContext().toString();
        }
        if (c3.callOnClick())
        {
            return c3.getContext().toString();
        }
        if (c4.callOnClick())
        {
            return c4.getContext().toString();
        }

        return null;
    }

}

log cat -

09-02 08:17:30.083: E/Trace(2148): error opening trace file: No such file or directory (2)
    09-02 08:17:30.193: D/dalvikvm(2148): GC_FOR_ALLOC freed 65K, 8% free 2413K/2620K, paused 26ms, total 28ms
    09-02 08:17:30.203: I/dalvikvm-heap(2148): Grow heap (frag case) to 4.553MB for 2160016-byte allocation
    09-02 08:17:30.313: D/dalvikvm(2148): GC_FOR_ALLOC freed 1K, 5% free 4521K/4732K, paused 104ms, total 104ms
    09-02 08:17:30.363: D/dalvikvm(2148): GC_CONCURRENT freed <1K, 5% free 4521K/4732K, paused 4ms+3ms, total 50ms
    09-02 08:17:30.832: D/gralloc_goldfish(2148): Emulator without GPU emulation detected.
    09-02 08:17:35.432: D/dalvikvm(2148): GC_FOR_ALLOC freed 17K, 4% free 4988K/5148K, paused 42ms, total 45ms
    09-02 08:17:35.482: I/dalvikvm-heap(2148): Grow heap (frag case) to 7.068MB for 2160016-byte allocation
    09-02 08:17:35.562: D/dalvikvm(2148): GC_CONCURRENT freed 4K, 3% free 7092K/7260K, paused 4ms+4ms, total 77ms
    09-02 08:17:36.512: I/Choreographer(2148): Skipped 764 frames!  The application may be doing too much work on its main thread.
    09-02 08:17:38.682: I/dalvikvm(2148): threadid=1: stack overflow on call to Landroid/view/View;.findViewById:LI
    09-02 08:17:38.682: I/dalvikvm(2148):   method requires 12+20+8=40 bytes, fp is 0x4205a320 (32 left)
    09-02 08:17:38.682: I/dalvikvm(2148):   expanding stack end (0x4205a300 to 0x4205a000)
    09-02 08:17:38.682: I/dalvikvm(2148): Shrank stack (to 0x4205a300, curFrame is 0x4205fec4)
    09-02 08:17:38.682: D/AndroidRuntime(2148): Shutting down VM
    09-02 08:17:38.682: W/dalvikvm(2148): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
    09-02 08:17:39.092: E/AndroidRuntime(2148): FATAL EXCEPTION: main
    09-02 08:17:39.092: E/AndroidRuntime(2148): java.lang.StackOverflowError
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.ViewGroup.findViewTraversal(ViewGroup.java:3053)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.findViewById(View.java:15104)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.ViewGroup.findViewTraversal(ViewGroup.java:3053)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.findViewById(View.java:15104)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.ViewGroup.findViewTraversal(ViewGroup.java:3053)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.findViewById(View.java:15104)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.ViewGroup.findViewTraversal(ViewGroup.java:3053)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.findViewById(View.java:15104)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.Window.findViewById(Window.java:900)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.app.Activity.findViewById(Activity.java:1839)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.starchazer.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:144)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.starchazer.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.starchazer.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.starchazer.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.starchazer.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.starchazer.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.V
    09-02 08:17:39.122: D/dalvikvm(2148): GC_CONCURRENT freed 762K, 11% free 7907K/8812K, paused 69ms+5ms, total 117ms
    09-02 08:17:39.122: D/dalvikvm(2148): WAIT_FOR_CONCURRENT_GC blocked 16ms
    09-02 08:22:39.222: I/Process(2148): Sending signal. PID: 2148 SIG: 9
    09-02 08:22:39.783: E/Trace(2182): error opening trace file: No such file or directory (2)
    09-02 08:22:39.883: D/dalvikvm(2182): GC_FOR_ALLOC freed 68K, 9% free 2413K/2624K, paused 25ms, total 27ms
    09-02 08:22:39.893: I/dalvikvm-heap(2182): Grow heap (frag case) to 4.553MB for 2160016-byte allocation
    09-02 08:22:40.013: D/dalvikvm(2182): GC_FOR_ALLOC freed 1K, 5% free 4521K/4736K, paused 114ms, total 114ms
    09-02 08:22:40.063: D/dalvikvm(2182): GC_CONCURRENT freed <1K, 5% free 4521K/4736K, paused 4ms+3ms, total 49ms
    09-02 08:22:40.513: D/gralloc_goldfish(2182): Emulator without GPU emulation detected.

在上面的代码中,我正在从数据库中检索四个答案按钮的数据。我的数据显示在按钮上。当我点击按钮我想检查我的答案是对还是错。并在回答了下一个问题之后,无论是对还是错。

3 个答案:

答案 0 :(得分:1)

@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;
}

上面的代码是检索被单击的视图并提取它的标题(文本),然后根据正确的答案进行检查。当然,既然你也可以不点击其中一个按钮,你可能需要检查点击的视图是否确实是其中一个按钮,如果没有,你应该跳过答案检查 - 我在上面的示例代码中省略了这个。 / p>

答案 1 :(得分:0)

将答案放在字符串变量&amp;检查按钮对象文本是否与字符串变量相同,而不是其他错误。

String str = <your answer> ; 
if(<btn object>.getText().toString().equalsTo(str))
  // correct answer
else
  // wrong answer


public class QuestionActivity extends Activity implements OnClickListener{

private Question currentQ;
private GamePlay currentGame;
private String str = "" ; // String to hold question

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.question);
    /**
     * Configure current game and get question
     */
    currentGame = ((CYKApplication)getApplication()).getCurrentGame();
    currentQ = currentGame.getNextQuestion();
    Button nextBtn1 = (Button) findViewById(R.id.answer1);
    nextBtn1.setOnClickListener(this);
    Button nextBtn2 = (Button) findViewById(R.id.answer2);
    nextBtn2.setOnClickListener(this);
    Button nextBtn3 = (Button) findViewById(R.id.answer3);
    nextBtn3.setOnClickListener(this);
    Button nextBtn4 = (Button) findViewById(R.id.answer4);
    nextBtn4.setOnClickListener(this);
    /**
     * Update the question and answer options..
     */
    setQuestions();

}


/**
 * Method to set the text for the question and answers from the current games
 * current question
 */
private void setQuestions() {
    //set the question text from current question
    String question = Utility.capitalise(currentQ.getQuestion());
    // assign correct answer to str variable.
    str = answer ; 
    TextView qText = (TextView) findViewById(R.id.question);
    qText.setText(question);

    //set the available options
    List<String> answers = currentQ.getQuestionOptions();
    TextView option1 = (TextView) findViewById(R.id.answer1);
    option1.setText(Utility.capitalise(answers.get(0)));

    TextView option2 = (TextView) findViewById(R.id.answer2);
    option2.setText(Utility.capitalise(answers.get(1)));

    TextView option3 = (TextView) findViewById(R.id.answer3);
    option3.setText(Utility.capitalise(answers.get(2)));

    TextView option4 = (TextView) findViewById(R.id.answer4);
    option4.setText(Utility.capitalise(answers.get(3)));
}


@Override
public void onClick(View arg0) {
    //Log.d("Questions", "Moving to next question");

if(!checkAnswer()) return;
    /**
     * check if end of game
     */
    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();
    }
}




@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    switch (keyCode)
    {
    case KeyEvent.KEYCODE_BACK :
        return true;
    }

    return super.onKeyDown(keyCode, event);
}


/*
  Check if a checkbox has been selected, and if it
  has then check if its correct and update gamescore
 */
private boolean checkAnswer() {
    String answer = getSelectedAnswer();
    if (answer==null){
        //Log.d("Questions", "No Checkbox selection made - returning");
        return false;
    }
    else {
        //Log.d("Questions", "Valid Checkbox selection made - check if correct");
        if (currentQ.getAnswer().equalsIgnoreCase(answer))
        {
            //Log.d("Questions", "Correct Answer!");
            currentGame.incrementScore();
        }
        else{
            //Log.d("Questions", "Incorrect Answer!");
            currentGame.decrementScore();
        }
        return true;
    }
}



private String getSelectedAnswer() {
    Button c1 = (Button)findViewById(R.id.answer1);
    Button c2 = (Button)findViewById(R.id.answer2);
    Button c3 = (Button)findViewById(R.id.answer3);
    Button c4 = (Button)findViewById(R.id.answer4);
    if (c1.callOnClick())
    {
        return c1.getContext().toString();
    }
    if (c2.callOnClick())
    {
        return c2.getContext().toString();
    }
    if (c3.callOnClick())
    {
        return c3.getContext().toString();
    }
    if (c4.callOnClick())
    {
        return c4.getContext().toString();
    }

    return null;
}

}

答案 2 :(得分:0)

尝试更改

c2.getContext().toString();

 c2.getText().toString();

所以这应该是

 private String getSelectedAnswer() {
    Button c1 = (Button)findViewById(R.id.answer1);
    Button c2 = (Button)findViewById(R.id.answer2);
    Button c3 = (Button)findViewById(R.id.answer3);
    Button c4 = (Button)findViewById(R.id.answer4);
    if (c1.callOnClick())
    {
        return c1.getText().toString();
    }
    if (c2.callOnClick())
    {
        return c2.getText().toString();
    }
    if (c3.callOnClick())
    {
        return c3.getText().toString();
    }
    if (c4.callOnClick())
    {
        return c4.getText().toString();
    }

    return null;
}