Text View Countdown

My goal is simple:

In my XML file, I have a text view: textView2.

I need a countdown, which is a countdown from 15 to 0, and every time a second passes, the text message gets an update (for example: 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. Sort of

If the countdown timer is on second 14, do it ...

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 application crashes! What's wrong?!

Log:

11-01 13: 09: 33.029: WARN / dalvikvm (388): threadid = 1: exit the thread with an 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: cannot start Events ComponentInfo {think.smart/think.smart.ThinkyoursmartActivity}: java.lang.NullPointerException 11-01 13: 09 : 33.049: ERROR / AndroidRuntime (388): with android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1647) 11-01 13: 09: 33.049: ERROR / AndroidRuntime (388): with android.app.ActivityThread.handleLaunchActivity ( ActivityThread.java:1663) 11-01 13: 09: 33.049: ERROR / AndroidRuntime (388): when android.app.ActivityThread.access $ 1500 (ActivityThread.java:117) 11-01 13: 09: 33.049: ERROR / AndroidRuntime (388): with android.app.ActivityThread $ H.handleMessage (ActivityTh read.java:931) 11-01 13: 09: 33.049: ERROR / AndroidRuntime (388): with android.os.Handler.dispatchMessage (Handler.java:99) 11-01 13: 09: 33.049: ERROR / AndroidRuntime ( 388): when android.os.Looper.loop (Looper.java:123) 11-01 13: 09: 33.049: ERROR / AndroidRuntime (388): when android.app.ActivityThread.main (ActivityThread.javahaps683) 11 -01 13: 09: 33.049: ERROR / AndroidRuntime (388): with java.lang.reflect.Method.invokeNative (native method) 11-01 13: 09: 33.049: ERROR / AndroidRuntime (388): with java.lang. reflect.Method.invoke (Method.java:507) 11-01 13: 09: 33.049: ERROR / AndroidRuntime (388): with com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:839) 11 -01 13: 09: 33.049: ERROR / AndroidRuntime (388): when com.android.internal.os.ZygoteInit.main (ZygoteInit.java∗97) 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): called: java.lang.NullPointerException 11-01 13: 09: 33.049: ERROR / AndroidRuntime (388): when think.smart.ThinkyoursmartActivity.onCreate (ThinkyoursmartActivity.java:34) 11-01 13: 09: 33.049: ERROR / AndroidRuntime (388): when android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1047) 11-01 13: 09: 33.049: ERROR / AndroidRuntime (388): when android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1611)

+6
source share
2 answers

Try the following: ----

 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(); 
+15
source

I think it is a matter of updating the user interface element due to the user interface flow.

try the following:

 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(); 
+1
source

Source: https://habr.com/ru/post/900510/


All Articles