1
06-06 23:50:28.340 27670-27706/mys.timer E/AndroidRuntime: FATAL EXCEPTION: Timer-0
Process: mys.timer, PID: 27660
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

My code:

protected void onCreate(Bundle savedInstanceState) {
 public void run() {
                new Timer().scheduleAtFixedRate(new TimerTask(){
                    @Override
                    public void run(){
    status.setText(getResources().getString(R.string.timercount));
    status.setTextColor(Color.parseColor("#990000"));

}
}
}

Please tell me how can i avoid my app from closing itself

  • Please check this question https://stackoverflow.com/questions/11123621/running-code-in-main-thread-from-another-thread – argoth Jun 06 '18 at 21:00

1 Answers1

1

Android’s UI components are not thread safe so one may need to update Views or other UI components from a secondary thread when returning from an asynchronous database query or a web service call. If you run the code from a secondary thread you might see that the code crashes almost on each try.

runOnUIThread Activity’s method

yourActivity.runOnUiThread(new Runnable(){
     public void run() {
          // UI code goes here
     }
});

I hope that it helps you.

argoth
  • 1,173
  • 8
  • 16