Search code examples
javaandroidtimer

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


I can't solve this error: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

TextView score;
private SharedPreferences speicher;
private SharedPreferences.Editor editor;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    score = (TextView) findViewById(R.id.score);
    speicher = getApplicationContext().getSharedPreferences("Daten", 0);
    editor = speicher.edit();
    loadfile("score" , score);

    new Timer().scheduleAtFixedRate(new TimerTask() {
        public void run() {
            Integer scorealt = Integer.parseInt(speicher.getString("score", null));
            Integer scorenewe = scorealt + Integer.parseInt(speicher.getString("anz", null));
            score.setText(scorenewe.toString());
            savefile("score", scorenewe.toString());
        }
    }, 0, 2000);
}

And I can't change score. score.setText(scorenewe.toString()); in line 45

android.view.ViewRootImpl$CalledFromWrongThreadException: 
    Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7769)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1332)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:5446)
at android.view.View.invalidateInternal(View.java:14750)
at android.view.View.invalidate(View.java:14714)
at android.view.View.invalidate(View.java:14698)
at android.widget.TextView.checkForRelayout(TextView.java:8535)
at android.widget.TextView.setText(TextView.java:5076)
at android.widget.TextView.setText(TextView.java:4901)
at android.widget.TextView.setText(TextView.java:4876)
at de.yt.tutorial.Home$1.run(Home.java:45)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)

Solution

  • It is because you're trying to touch the view while not in the UI thread.

    Quick fix looks like this:

    code before

    score.setText(scorenewe.toString());
    

    code after:

    new Handler(Looper.getMainLooper()).post(new Runnable(){
        @Override
        public void run() {
            score.setText(scorenewe.toString());
        }
    });
    

    This way you will tell Android framework to run this line of code in the main UI thread, where you can touch any view you want P.S. read this