使用countdowntimer.cancel()时应用程序崩溃

时间:2017-10-26 08:45:48

标签: android

我想创建一个运行10秒钟的计时器。如果用户输入了正确答案,则应重新启动计时器。为此,我添加了cancel.countdowntimer()。但它不起作用。该应用程序崩溃。我应该在哪里使用countdowntimer.cancel()函数?我应该使用countdowntimer.cancel()的方法吗?

 package com.example.vignesh.work;

    import android.os.CountDownTimer;
    import android.os.Handler;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    import java.util.ArrayList;
    import java.util.Random;

    public class MainActivity extends AppCompatActivity {
        Button b1;
        int loc;
            int total=0;
        int mark=0;
        Runnable run;
        Handler handler;
        CountDownTimer countDownTimer;
        TextView textView4,textView2,textView1,textView3;
        Button b11,b22,b33,b44;
        Random random = new Random();
        ArrayList<Integer> answers = new ArrayList<Integer>();

            public void nextQuestion() {
                new CountDownTimer(10000 + 100, 1000) {

                    @Override
                    public void onTick(long millisUntilFinished) {

                        textView3.setText(Long.toString(millisUntilFinished / 1000));
                    }

                    @Override
                    public void onFinish() {
                        textView3.setText("10");
                    }
                }.start();

                int a = random.nextInt(20) + 1;
                int b = random.nextInt(20) + 1;
                textView1.setText(Integer.toString(a) + "+" + Integer.toString(b));
                loc = random.nextInt(4);
                answers.clear();
                for (int i = 0; i < 4; i++) {
                    if (i == loc) {
                        answers.add(a + b);
                    } else {
                        answers.add((a + b) + random.nextInt(10) + 1);
                    }
                }
                b11.setText(Integer.toString(answers.get(0)));
                b22.setText(Integer.toString(answers.get(1)));
                b33.setText(Integer.toString(answers.get(2)));
                b44.setText(Integer.toString(answers.get(3)));
            }  



        public void choose(View view)
        {
          if(view.getTag().toString().equals(Integer.toString(loc+1)))
            {
             textView4.setText("Correct");
                mark++;

            }
            else
            {
               textView4.setText("Wrong");

            }
            total++;
            try {
                countDownTimer.cancel();
            }
            catch (Exception e)
            {
                Log.i("error",e.getMessage());
            }
            nextQuestion();
            textView2.setText(Integer.toString(mark)+"/"+Integer.toString(total));
        }

        public void click(View view)
        {
            b1.setVisibility(view.INVISIBLE);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            b1 = (Button)findViewById(R.id.buttongo);
            b11 = (Button)findViewById(R.id.optiona);
            b22 = (Button)findViewById(R.id.optionb);
            b33 = (Button)findViewById(R.id.option3);
            b44 = (Button)findViewById(R.id.option4);
            textView1=(TextView)findViewById(R.id.textView1);
            textView2=(TextView)findViewById(R.id.textView2);
            textView3=(TextView)findViewById(R.id.textView3);
            textView4=(TextView)findViewById(R.id.textView4);
            nextQuestion();
        }
    }
**AFTER ADDING EXCEPTIONAL HANDLING THE ERROR WAS:Attempt to invoke virtual method 'void android.os.CountDownTimer.cancel()' on a null object reference**

感谢。

2 个答案:

答案 0 :(得分:0)

您没有初始化countDownTimer变量更新您的方法,如下所示,并在调用countDownTimer.cancel()之前检查null;

public void nextQuestion() {
              countDownTimer =  new CountDownTimer(10000 + 100, 1000) {

                    @Override
                    public void onTick(long millisUntilFinished) {

                        textView3.setText(Long.toString(millisUntilFinished / 1000));
                    }

                    @Override
                    public void onFinish() {
                        textView3.setText("10");
                    }
                }.start();

                int a = random.nextInt(20) + 1;
                int b = random.nextInt(20) + 1;
                textView1.setText(Integer.toString(a) + "+" + Integer.toString(b));
                loc = random.nextInt(4);
                answers.clear();
                for (int i = 0; i < 4; i++) {
                    if (i == loc) {
                        answers.add(a + b);
                    } else {
                        answers.add((a + b) + random.nextInt(10) + 1);
                    }
                }
                b11.setText(Integer.toString(answers.get(0)));
                b22.setText(Integer.toString(answers.get(1)));
                b33.setText(Integer.toString(answers.get(2)));
                b44.setText(Integer.toString(answers.get(3)));
            }  

答案 1 :(得分:0)

您在CountDownTimer方法中实例化了nextQuestion(),但是您没有在班级顶部定义的countDownTimer变量中指定它:CountDownTimer countDownTimer;

nextQuestion()方法中的代码更改为:

countDownTimer = new CountDownTimer(10000 + 100, 1000) {

    @Override
    public void onTick(long millisUntilFinished) {

        textView3.setText(Long.toString(millisUntilFinished / 1000));
    }

    @Override
    public void onFinish() {
        textView3.setText("10");
    }
};
countDownTimer.start();

请注意,我正在将new CountDownTimer分配给countDownTimer变量,然后通过调用countDownTimer.start();启动计时器。

然后在调用取消之前检查countDownTimer是否为空:

if(countDownTimer != null) {
    countDownTimer.cancel();
}