0

我的 onTick() 方法中的 2 行出现 NullPointer 错误。我是实现子类以及如何让这个计时器工作的新手。我刚刚搜索了 CountDownTimers 并尝试实现我发现的一个示例,因此它可能有点混乱。

在我实现计时器之前,该应用程序运行良好。我想要的是让计时器在此活动开始后立即启动,然后在单击或计时器用完后重新启动计时器并显示新问题。

先感谢您!

public class QuestionView extends Activity  {

    int correctAnswers = 0;
    int wrongAnswers = 0;

    int answer = 0;

    int i = 0;

    long startTime = 50000;
    long interval = 1000;
    long timeElapsed;

    boolean timerHasStarted = false;

    Button answer1;
    Button answer2;
    Button answer3;
    Button answer4;
    TextView question;
    TextView timer;
    TextView timeElapsedView;

    ArrayList<Question> queries;
    Timer cdTimer;

    @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");

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

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

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

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

    public int getCorrectAnswers() { return correctAnswers; }
    public int getWrongAnswers() { return wrongAnswers; }

    public ArrayList<Question> getQueries() {
        return queries;
    }

    public void nextQuestion() {
        i++;
        loadQuestion();
    }

    public class Timer extends CountDownTimer {

        public Timer(long starttime, long interval) {
            super(startTime, interval);
        }

        @Override
        public void onFinish() {
            timer.setText("Time's up!");
            timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
        }

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

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


}

日志猫

12-05 07:32:06.142: E/AndroidRuntime(6264): FATAL EXCEPTION: main
12-05 07:32:06.142: E/AndroidRuntime(6264): java.lang.NullPointerException
12-05 07:32:06.142: E/AndroidRuntime(6264):     at com.example.test.QuestionView$Timer.onTick(QuestionView.java:164)
12-05 07:32:06.142: E/AndroidRuntime(6264):     at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
12-05 07:32:06.142: E/AndroidRuntime(6264):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 07:32:06.142: E/AndroidRuntime(6264):     at android.os.Looper.loop(Looper.java:137)
12-05 07:32:06.142: E/AndroidRuntime(6264):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-05 07:32:06.142: E/AndroidRuntime(6264):     at java.lang.reflect.Method.invokeNative(Native Method)
12-05 07:32:06.142: E/AndroidRuntime(6264):     at java.lang.reflect.Method.invoke(Method.java:511)
12-05 07:32:06.142: E/AndroidRuntime(6264):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-05 07:32:06.142: E/AndroidRuntime(6264):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-05 07:32:06.142: E/AndroidRuntime(6264):     at dalvik.system.NativeStart.main(Native Method)
4

4 回答 4

1

这可能不是唯一的问题,但您还没有初始化timertimeElapsedView.

你需要这样的东西onCreate()

timer = (TextView) findViewById(R.id.timer);
timeElapsedView = (TextView) findViewById(R.id.timeelapsedView);
于 2012-12-10T19:48:35.300 回答
0

我看不到您在哪里初始化 TextView

timeElapsedView = (TextView) findViewById(R.id.idOfYourTextView)

因此,当您尝试设置文本finish()并且onTick()没有设置文本时

于 2012-12-10T19:49:25.357 回答
0

计时器从不初始化。所以它是空的,所以你得到一个 NPE。

于 2012-12-10T19:49:35.107 回答
0

你也应该改变

public Timer(long starttime, long interval) {
            super(startTime, interval);
        }

public Timer(long startTime, long interval) {
            super(startTime, interval);
        }
于 2012-12-10T19:51:51.233 回答