简体   繁体   中英

CountDownTimer calls double method

I don't know how to explain better. I have this timer, and after it finish counting it should call another class (popup) and after that other function in the same class where the counter is.

public class MyCount extends CountDownTimer {

        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }


        public void onFinish() {

            Intent i = new Intent(getApplicationContext(), PogresanOdgovor.class);
            i.putExtra("tacanOdgovor", tacanOdg);
            startActivity(i);
            mHandler.postDelayed(mLaunchTask,2200);

            brojacPogresnihOdgovora++;
        }

After first pass, my score is 2 instead of 1, then 6, the 14...This delayed method is simply the next question:

Runnable mLaunchTask = new Runnable() {
        public void run() {
            nextQuestion();
            brojacVremena.start();
        }
     };

I call exactly the same method as the one in onFinish() when user answer wrong and it works fine.

MyCount brojacVremena = new MyCount(6000, 1000);

final OnClickListener clickListener = new OnClickListener() { public void onClick(View v) {

        Answer ans = (Answer) v.getTag();
        if (ans.isCorrect) {
            brojacVremena.cancel();
            brojacTacnihOdgovora = brojacTacnihOdgovora + 5;
            Intent i = new Intent("rs.androidaplikacijekvizopstekulture.TACANODGOVOR");
            startActivity(i);
            mHandler.postDelayed(mLaunchTask,1200);
        }
    else{
        brojacVremena.cancel();
        brojacPogresnihOdgovora++;
        Intent i = new Intent(getApplicationContext(), PogresanOdgovor.class);
        i.putExtra("tacanOdgovor", tacanOdg);
        startActivity(i);
        mHandler.postDelayed(mLaunchTask,2200);

    }
};

I found my error. I called my counter twice. Here:

nextQuestion(); 
brojacVremena.start();

and below in the very same nextQuestion method:

public void nextQuestion() {
brojacVremena.start();
.
.
.

I don't know how that happened.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM