2012-06-16 19 views
11

Agregué esta parte del código en mi método onCreate() y bloquea mi aplicación. necesita ayuda.aplicación que se bloquea con "Llamada desde la excepción de subproceso erróneo"

LogCat:

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread 
that created a view hierarchy can touch its views. 

CÓDIGO:

final TextView timerDisplayPanel = (TextView) findViewById(R.id.textView2); 

    Timer t = new Timer(); 
    t.schedule(new TimerTask(){ 
     public void run(){ 
      timerInt++; 
      Log.d("timer", "timer"); 
      timerDisplayPanel.setText("Time ="+ timerInt +"Sec"); 
     } 
    },10, 1000); 
+0

(No tengo ni idea, cómo usar un controlador.) - Leí en algún lugar que necesito usar un objeto controlador. \ –

Respuesta

32
Only the original thread that created a view hierarchy can touch its views. 

Usted está tratando de cambiar el texto del elemento de interfaz de usuario en No IU hilo, por lo que da una excepción. Use runOnUiThread

Timer t = new Timer(); 
t.schedule(new TimerTask() { 
public void run() { 
     timerInt++; 
     Log.d("timer", "timer"); 

     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       timerDisplayPanel.setText("Time =" + timerInt + "Sec"); 
      } 
     }); 

    } 
}, 10, 1000); 
Cuestiones relacionadas