如何解决与分数相关的错误并进入android常规知识测验中的下一轮?

时间:2013-09-03 10:07:57

标签: android button scoring

我正在进行Android测验。当我在模拟器上运行它第一个问题出现,我回答它。它停止了工作。然后从头重新开始。我不知道我的代码中的问题在哪里。

questionactivity.class

import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


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 (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.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;
    }

}

日志输出:

    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.

当我从onClick()方法中删除对checkAnswer()方法的调用时,我的游戏工作。但后来我的游戏没有检查我的答案是对还是错。另一方面,如果我不删除对此方法的调用。我的游戏工作,在回答第一个问题后,我的应用程序停止工作并重新启动有人可以帮助我在我的代码中找到错误吗?

1 个答案:

答案 0 :(得分:1)

onClick为您提供了点击的视图(代码中为arg0),因此将其传递给您的checkAnswer方法。然后在checkAnswer(或getSelectedAnswer)中,您只需检查点击了哪个视图,然后直接使用它来验证答案。

您现在使用的方法可能会导致某种递归,因为您为onClick中的每个按钮调用getSelectedAnswer并导致堆栈溢出。


编辑:示例代码(未经测试)来说明这个想法

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

由于我不确切知道您的应用是如何布局的,我假设您有四个“按钮”,每个按钮设置一个答案,因为它的标题。上面的代码是检索被单击的视图并提取它的标题(文本),然后根据正确的答案进行检查。当然,既然你也可以点击其中一个按钮,你可能需要检查点击的视图是否确实是其中一个按钮,如果没有,你应该跳过答案检查 - 我省略了这个在上面的示例代码中。