1

我有一个显示 10 个问题的问答游戏,每个问题都有一个 10 秒的计时器。我有 2 个无法正常工作的问题。

首先,如果计时器在一个问题上用完,它会显示下一个问题,但计时器不会重置。文本视图停留在“时间到了!” 和“经过的时间:10000”,而不是在显示的新问题上重新启动计时器。

最后,在结果页面上,正确的分数不会显示在文本视图中。百分比 textview 显示正确,但得分 textview 显示“android.widget.TextView@416473c”或其他一些随机内存位置。

该程序永远不会崩溃,只是功能不正确。非常感谢任何代码结构或其他建议!这是我第一次尝试安卓移动应用程序,我正在缓慢而艰难地完成它。还是很享受!:)

QuestionView.java

public class QuestionView extends Activity  {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.questionviewmain);

        answer1 = (Button)findViewById(R.id.answer1);
        answer2 = (Button)findViewById(R.id.answer2);
        answer3 = (Button)findViewById(R.id.answer3);
        answer4 = (Button)findViewById(R.id.answer4);

        question = (TextView)findViewById(R.id.question);
        queries = getIntent().getParcelableArrayListExtra("queries");
        timer = (TextView)findViewById(R.id.timer);
        timeElapsedView = (TextView)findViewById(R.id.timeElapsedView);
        cdTimer = new Timer(startTime, interval);
        loadQuestion();
    }

    public void loadQuestion() {

        if(i == 9) {    
            endQuiz();  
        } else {    
            if(!timerHasStarted) {
                cdTimer.start();
                timerHasStarted = true;
            } else {
                cdTimer.cancel();
                timerHasStarted = false;
            }
            answer = queries.get(i).getCorrectAnswer();
            question.setText(queries.get(i).getQuery());

            answer1.setText(queries.get(i).getA1());
            answer2.setText(queries.get(i).getA2());
            answer3.setText(queries.get(i).getA3());
            answer4.setText(queries.get(i).getA4());

            answer1.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    queries.get(i).setSelectedAnswer(0);
                    if(answer == 0) {
                        correctAnswers++;
                        nextQuestion();
                    } else {
                        wrongAnswers++;
                        nextQuestion();
                    }
                }
            });

           //same type of code for buttons for answers 2 through 4.
                } 
    }

    public void nextQuestion() {
        score = score + timeElapsed;
        i++;
        loadQuestion();
    }

    public class Timer extends CountDownTimer {     
        public Timer(long startTime, long interval) {
            super(startTime, interval);
        }

        public void onFinish() {
            if(i == 9) {
                cdTimer.cancel();
            } else {
                timer.setText("Time's up!");
                timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
                wrongAnswers++;
                nextQuestion();
            }
        }

        public void onTick(long millisUntilFinished) {
            timer.setText("Time remain: " + Long.toString(millisUntilFinished));
            timeElapsed = startTime - millisUntilFinished;
            timeElapsedView.setText("Time Elapsed: " + Long.toString(timeElapsed));
        }
    }

    public void endQuiz() {
        Intent intent = new Intent(QuestionView.this, Results.class);
        intent.putExtra("correctAnswers", correctAnswers);
        intent.putExtra("wrongAnswers", wrongAnswers);
        intent.putExtra("score", score);
        intent.putParcelableArrayListExtra("queries", queries);
        startActivity(intent);
    }
}

结果.java

    public class Results extends Activity {

    QuestionView qv = new QuestionView();
    ArrayList<Question> queryList = qv.getQueries();

    int cAnswers;
    int wAnswers;

    long score;

    ArrayList<Question> qs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resultsmain);

        cAnswers = getIntent().getIntExtra("correctAnswers", -1);
        wAnswers = getIntent().getIntExtra("wrongAnswers", -1);
        score = getIntent().getLongExtra("score", -1);

        qs = getIntent().getParcelableArrayListExtra("queries");

        Button mainmenuBtn = (Button)findViewById(R.id.mainmenuBtn);
        mainmenuBtn.setText("Main Menu");

        mainmenuBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                restart();
            }
        });

        showResults();
    }

    public void showResults() {

        ArrayList<TextView> tList = new ArrayList<TextView>(9);

        TextView header = (TextView)findViewById(R.id.header);
        header.setText("SUMMARY");

        TextView percentage = (TextView)findViewById(R.id.percentage);
        percentage.setText(Integer.toString(10 * cAnswers) + "%");

        TextView score = (TextView)findViewById(R.id.score);
        String s = "" + score;
        score.setText(s);

        TextView q1 = (TextView)findViewById(R.id.q1);
        TextView q2 = (TextView)findViewById(R.id.q2);
        TextView q3 = (TextView)findViewById(R.id.q3);
        TextView q4 = (TextView)findViewById(R.id.q4);
        TextView q5 = (TextView)findViewById(R.id.q5);
        TextView q6 = (TextView)findViewById(R.id.q6);
        TextView q7 = (TextView)findViewById(R.id.q7);
        TextView q8 = (TextView)findViewById(R.id.q8);
        TextView q9 = (TextView)findViewById(R.id.q9);
        TextView q10 = (TextView)findViewById(R.id.q10);

        tList.add(q1);
        tList.add(q2);
        tList.add(q3);
        tList.add(q4);
        tList.add(q5);
        tList.add(q6);
        tList.add(q7);
        tList.add(q8);
        tList.add(q9);
        tList.add(q10);

        for(int i = 0; i < tList.size(); i++) {
            tList.get(i).setText(qs.get(i).getQuery());
            if(qs.get(i).getSelectedAnswer() == qs.get(i).getCorrectAnswer()) {
                tList.get(i).setTextColor(Color.GREEN);
            } else {
                tList.get(i).setTextColor(Color.RED);
            }
        }
    }

    public void restart() {
        Intent intent = new Intent(Results.this, MainMenu.class);
        startActivity(intent);
    }
}
4

2 回答 2

4

从所有这些代码中,这就是我认为正在发生的事情

首先,如果计时器在一个问题上用完,它会显示下一个问题,但计时器不会重置。文本视图停留在“时间到了!” 和“经过的时间:10000”,而不是在显示的新问题上重新启动计时器。

这似乎是由于您没有timerHasStarted在时间用完后将变量设置为 false,所以我可能会在您加载下一个问题或显示结果后将其设置为 false。

最后,在结果页面上,正确的分数不会显示在文本视图中。百分比 textview 显示正确,但得分 textview 显示“android.widget.TextView@416473c”或其他一些随机内存位置。

这是因为您将 q 变量设置为 textview 并获取 id。你需要类似的东西q1.getText().toString()

您有多个具有相同名称分数的变量。所以改成

  TextView score2 = (TextView)findViewById(R.id.score);
  String s = "" + score;
  score2.setText(s);
于 2012-12-17T16:35:38.350 回答
0

分数显示的不是您所期望的,因为您分配的String s = "" + scorewherescore是您命名的Textview,显然不是整数,也不等于用户的分数。:)

于 2013-02-23T15:50:03.053 回答