Jon Sn0w
Jon Sn0w

Reputation: 3

Changing the label on a edittext

So i have implemented a timer that calls a timertask extension called labelsTimer. The method should change the label of the text to that stipulated in the code but instead throws a error

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

this is the code

public class labelsTimer extends TimerTask {
EditText label;

labelsTimer(EditText label)
{   super();
    this.label = label;
}
@Override
public void run()
{

    System.out.print("fsdfsd");
    label.setText("hehehehe");
}

}

Upvotes: 0

Views: 50

Answers (2)

Deepak Goyal
Deepak Goyal

Reputation: 4907

Use this

public void run(){
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        label.setText("hehehehe");
      }
    });
}

Upvotes: 1

Marcin D
Marcin D

Reputation: 916

change the label in this wrapper thread

try {
    runOnUiThread(new Runnable() {

         @Override
         public void run() {
             label.setText("hehehehe");
         }
    });          
} catch (InterruptedException e) {
     e.printStackTrace();
}

Upvotes: 0

Related Questions