用textview倒计时

use*_*311 8 android

我的目标很简单:

在我的xml文件中,我有一个名为textView2的textview.

我需要的是倒计时,倒计时从15到0,每次一秒钟,textview得到更新(如:15,14,13,12,11,10,9,8,7,6,5, 4,3,2,1,0).

而且我还需要从中获取当前时间.等等

如果倒数计时器是第14个,那么这样做......

我试过这个:

   new CountDownTimer(30000, 1000) {

        public void onTick(long millisUntilFinished) {
            int j = (int) millisUntilFinished;
            TextView textic = (TextView) findViewById(R.id.textView2);
            textic.setText(j);
        }

        public void onFinish() {

        }
     }.start();
Run Code Online (Sandbox Code Playgroud)

但应用程序崩溃了!怎么了?!

日志:

11-01 13:09:33.029:WARN/dalvikvm(388):threadid = 1:线程退出未捕获异常(组= 0x40015560)11-01 13:09:33.049:错误/ AndroidRuntime(388):致命异常:主11-01 13:09:33.049:ERROR/AndroidRuntime(388):java.lang.RuntimeException:无法启动活动ComponentInfo {think.smart/think.smart.ThinkyoursmartActivity}:java.lang.NullPointerException 11-01 13:09 :33.049:ERROR/AndroidRuntime(388):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)11-01 13:09:33.049:ERROR/AndroidRuntime(388):at android.app.ActivityThread.handleLaunchActivity( ActivityThread.java:1663)11-01 13:09:33.049:ERROR/AndroidRuntime(388):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)11-01 13:09:33.049:ERROR/AndroidRuntime (388):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)11-01 13:09:33.049:ERROR/AndroidRuntime(388):在android.os.Handler.dispatchMessage(Handler.java: 99)11-01 13:09:33.049:ERROR/AndroidRuntime(388 ):在android.os.Looper.loop(Looper.java:123)11-01 13:09:33.049:ERROR/AndroidRuntime(388):在android.app.ActivityThread.main(ActivityThread.java:3683)11- 01 13:09:33.049:ERROR/AndroidRuntime(388):at java.lang.reflect.Method.invokeNative(Native Method)11-01 13:09:33.049:ERROR/AndroidRuntime(388):at java.lang.reflect .Method.invoke(Method.java:507)11-01 13:09:33.049:ERROR/AndroidRuntime(388):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)11- 01 13:09:33.049:ERROR/AndroidRuntime(388):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)11-01 13:09:33.049:ERROR/AndroidRuntime(388):at dalvik.system.NativeStart.main(Native Method)11-01 13:09:33.049:ERROR/AndroidRuntime(388):引起:java.lang.NullPointerException 11-01 13:09:33.049:ERROR/AndroidRuntime(388) :at think.smart.ThinkyoursmartActivity.onCreate(ThinkyoursmartActivity.java:34)11-01 13:09:33.049:ERROR/AndroidRuntime(388):at android.app.Instrumentation.callActivityOn Create(Instrumentation.java:1047)11-01 13:09:33.049:ERROR/AndroidRuntime(388):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

Utt*_*tam 17

试试这个: - -

TextView textic = (TextView) findViewById(R.id.textView2);

CountDownTimer Count = new CountDownTimer(30000, 1000) {
    public void onTick(long millisUntilFinished) {
        textic.setText("Seconds remaining: " + millisUntilFinished / 1000);
    }

    public void onFinish() {
        textic.setText("Finished");
    }
};

Count.start();
Run Code Online (Sandbox Code Playgroud)

  • 我只能通过`@ Override`来获得onTick和onFinish方法.确实最好键入CountDownT <Tab>并让IDE为您完成所有样板.另外,按照惯例,`Count`变量应该是`count` - 它是一个变量而不是一个类. (3认同)