Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Countdown with textview

Tags:

android

My goal is simple:

In my xml file,I have a textview called: textView2.

What I need is a countdown,that countsdown from 15 to 0,and every time a second passes,the textview gets update(like: 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0).

And I will also need to get the current time from it.Something like

If the countdown timer is at second 14,then do this...

I tried this:

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

But the app crashes! What's wrong?!

Log:

11-01 13:09:33.029: WARN/dalvikvm(388): threadid=1: thread exiting with uncaught exception (group=0x40015560) 11-01 13:09:33.049: ERROR/AndroidRuntime(388): FATAL EXCEPTION: main 11-01 13:09:33.049: ERROR/AndroidRuntime(388): java.lang.RuntimeException: Unable to start activity ComponentInfo{think.smart/think.smart.ThinkyoursmartActivity}: java.lang.NullPointerException 11-01 13:09:33.049: ERROR/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: ERROR/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: 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): Caused by: 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)

like image 249
user1015311 Avatar asked Nov 01 '11 13:11

user1015311


1 Answers

Try this:----

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();
like image 50
Uttam Avatar answered Nov 09 '22 15:11

Uttam