Pergunta

I am using countdoen in my android quiz game. Some times it works really well & sometimes it gives force close. I am initializing 8 timers for 8 questions & also canceling it.All timers uses one common textview in onTick method.

switch (question_display_number) {
    case 1:
        startTime = 30 * 1000;
        q1 = new MyCountDownTimer(startTime, interval);
        q1.start();
        break;
    case 2:
        startTime = 30 * 1000;
        q2 = new MyCountDownTimer(startTime, interval);
        q2.start();
        break;
    case 3:
        startTime = 30 * 1000;
        q3 = new MyCountDownTimer(startTime, interval);
        q3.start();
        break;
    case 4:
        startTime = 30 * 1000;
        q4 = new MyCountDownTimer(startTime, interval);
        q4.start();
        break;
    case 5:
        startTime = 60 * 1000;
        q5 = new MyCountDownTimer(startTime, interval);
        q5.start();
        break;
    case 6:
        startTime = 60 * 1000;
        q6 = new MyCountDownTimer(startTime, interval);
        q6.start();
        break;
    case 7:
        startTime = 60 * 1000;
        q7 = new MyCountDownTimer(startTime, interval);
        q7.start();
        break;
    case 8:
        startTime = 60 * 1000;
        q8 = new MyCountDownTimer(startTime, interval);
        q8.start();
        break; 

here is the timer.

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

    @Override
    public void onTick(long millisUntilFinished) {
        timer.setText(""+millisUntilFinished / 1000);
      // timer is a TextView.
    }

    @Override
    public void onFinish() {
        // alert dialog with two buttons.           
    }

please help me, what should be the reason for sudden force close ? I know it's because of countDown timer, because my previous version game works fine, without force close.

Foi útil?

Solução

timer.setText(""+millisUntilFinished / 1000);

Where have you initialized timer? It will be null at this point and throw a NullPointerException

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top