用textview倒计时

时间:2011-11-01 13:00:44

标签: android

我的目标很简单:

在我的xml文件中,我有一个名为textView2的文本视图。

我需要的是倒计时,倒计时从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();

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

日志:

  

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:   错误/ AndroidRuntime(388):at   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):at   android.app.ActivityThread.access $ 1500(ActivityThread.java:117)11-01   13:09:33.049:ERROR / AndroidRuntime(388):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)   11-01 13:09:33.049:ERROR / AndroidRuntime(388):at   android.os.Handler.dispatchMessage(Handler.java:99)11-01   13:09:33.049:ERROR / AndroidRuntime(388):at   android.os.Looper.loop(Looper.java:123)11-01 13:09:33.049:   错误/ AndroidRuntime(388):at   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:   错误/ 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.callActivityOnCreate(Instrumentation.java:1047)   11-01 13:09:33.049:ERROR / AndroidRuntime(388):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:1)

我认为这是从UI线程外部更新UI元素的问题

请尝试以下方法:

new CountDownTimer(30000, 1000) {

public void onTick(long millisUntilFinished) {
    final int j = (int) millisUntilFinished;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            TextView textic = (TextView) findViewById(R.id.textView2);
            textic.setText(j);
        }
    });
    }

    public void onFinish() {        
    }
}.start();