2011-11-01 14 views
6

Il mio obiettivo è semplice:Countdown con TextView

Nel mio file xml, ho un TextView chiamato: textView2.

Quello che mi serve è un conto alla rovescia, che esegue il conto alla rovescia da 15 a 0, e ogni volta che passa un secondo, la visualizzazione del testo viene aggiornata (come: 15,14,13,12,11,10,9,8,7, 6,5,4,3,2,1,0).

E io anche bisogno di ottenere il tempo corrente da it.Something come

Se il conto alla rovescia è in seconda 14, poi fare questo ...

ho provato questo:

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

Ma l'app si blocca! Cosa c'è che non va?!

Log:

11-01 13: 09: 33,029: WARN/dalvikvm (388): threadid = 1: filetto uscendo con eccezione non rilevata (gruppo = 0x40015560) 11-01 13:09: 33,049: ERRORE/AndroidRuntime (388): eccezione irreversibile: main 11-01 13: 09: 33,049: ERRORE/AndroidRuntime (388): java.lang.RuntimeException: Impossibile avviare l'attività ComponentInfo {think.smart/think .smart.ThinkyoursmartActivity}: java.lang.NullPointerException 11-01 13: 09: 33.049: ERRORE/AndroidRuntime (388): a android.app.Ac tivityThread.performLaunchActivity (ActivityThread.java:1647) 11-01 13: 09: 33.049: ERRORE/AndroidRuntime (388): a android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:1663) 11-01 13:09 : 33.049: ERRORE/AndroidRuntime (388): a android.app.ActivityThread.access $ 1500 (ActivityThread.java:117) 11-01 13: 09: 33.049: ERRORE/AndroidRuntime (388): a android.app. ActivityThread $ H.handleMessage (ActivityThread.java:931) 11-01 13: 09: 33.049: ERRORE/AndroidRuntime (388): a android.os.Handler.dispatchMessage (Handler.java:99) 11-01 13 : 09: 33.049: ERRORE/AndroidRuntime (388): a android.os.Looper.loop (Looper.java:123) 11-01 13: 09: 33.049: ERRORE/AndroidR untime (388): a android.app.ActivityThread.main (ActivityThread.java:3683) 11-01 13: 09: 33.049: ERRORE/AndroidRuntime (388): a java.lang.reflect.Method.invokeNative (Metodo nativo) 11-01 13: 09: 33.049: ERRORE/AndroidRuntime (388): a java.lang.reflect.Method.invoke (Method.java:507) 11-01 13: 09: 33.049: ERRORE/AndroidRuntime (388): a com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:839) 11-01 13: 09: 33.049: ERRORE/AndroidRuntime (388): a com.android. internal.os.ZygoteInit.main (ZygoteInit.java:597) 11-01 13: 09: 33.049: ERRORE/AndroidRuntime (388): a dalvik.system.NativeStart.main (metodo nativo) 11-01 13:09 33.049: ERROR/AndroidRuntime (388): causato da: java.lang.NullPointerException 11-01 13: 09: 33,049: ERROR/AndroidRuntime (388): a think.smart.ThinkyoursmartActivity.onCreate (ThinkyoursmartActivity.java:34) 11-01 13: 09: 33.049: ERRORE/AndroidRuntime (388): a android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1047) 11-01 13: 09: 33.049: ERRORE/AndroidRuntime (388): a android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 1611)

+1

Dai un'occhiata a [CountDownTimers] (http://developer.android.com/reference/android/os/CountDownTimer.html). Questo probabilmente ti renderà la vita molto più facile. –

risposta

15

Prova questo: ----

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

Potrei solo far funzionare i metodi onTick e onFinish con '@ Override'. In realtà è meglio digitare CountDownT e lasciare che l'IDE completi tutto il boilerplate per te. Inoltre, come da convenzione, la variabile 'Count' dovrebbe essere' count' - è una variabile non una classe. – stackunderflow

1

Credo che sia la materia di aggiornamento di un elemento dell'interfaccia utente da fuori thread dell'interfaccia utente

così provare quanto segue:

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