Android倒计时器

时间:2016-03-02 12:14:46

标签: java android countdowntimer

我正在尝试安装倒数计时器,以便在一段时间过后,显示一个Toast并启用登录。但是,目前当我运行应用程序并且计数器达到值0时,我得到错误“不幸的是,应用已停止”。

Java代码

if (counter == 0) {


                    b1.setEnabled(false);
                    ed1.setEnabled(false);
                    ed2.setEnabled(false);
                    Toast.makeText(getApplicationContext(), "You have logged in incorrectly too many times...", Toast.LENGTH_SHORT).show();

                    // Countdown timer that counts down from 30, once timer reaches 0 counter is set back to 3 and login button and views enabled again
                    new CountDownTimer(30100, 1000) {

                        public void onTick(long millisUntilFinished) {
                            timer.setText("Seconds remaining: " + millisUntilFinished / 1000);

                        }

                        public void onFinish() {
                            timer.setText("");
                            Toast.makeText(getApplicationContext(), "Login features have been reactivated", Toast.LENGTH_SHORT).show();
                            b1.setEnabled(true);
                            ed1.setEnabled(true);
                            ed2.setEnabled(true);

                            counter = 3;
                            String CounterString = Integer.toString(counter);
                            tx1.setText(CounterString);

                        }
                    }
                            .start();
                }

            }
        }

    });

}}

logcat的:

03-02 12:10:41.992    1416-1416/com.example.scarlett.assignmenttwo E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.example.scarlett.assignmenttwo.MainActivity$1$1.onTick(MainActivity.java:89)
            at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案