0

I Want to create a timer which will run for 10 seconds. If the user have entered the correct answer the timer should restart. For that I have added cancel.countdowntimer(). But it's not working. The app crashes. Where should I use that countdowntimer.cancel() function? Should I use a method for 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**

Thanks.

Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
Vignesh G
  • 29
  • 2
  • post your logcat with the error please – Ivan Oct 26 '17 at 08:47
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Selvin Oct 26 '17 at 08:48

2 Answers2

1

You instantiate your CountDownTimer in nextQuestion() method, however you do not assign it in the countDownTimer variable defined in the top of your class: CountDownTimer countDownTimer;

Change the code inside nextQuestion() method to:

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

Note that I am assigning the new CountDownTimer to countDownTimer variable and then start the timer by calling countDownTimer.start();.

Then check if countDownTimer is null before calling it to cancel:

if(countDownTimer != null) {
    countDownTimer.cancel();
}
pleft
  • 7,567
  • 2
  • 21
  • 45
0

You are not initialising countDownTimer variable update your method like below and also check null before calling countDownTimer.cancel();

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)));
            }  
J Ramesh
  • 548
  • 4
  • 11