Android: only the original thread that created a view hierarchy can touch its views when calling invalidate()

Ryan picture Ryan · Jul 31, 2017 · Viewed 14.6k times · Source

I'm trying to play a gif using the Movie object and it requires me to call the invalidate() method. However whenever I call this method I get the following error:

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

How do I fix this and why is it happening

Answer

redAllocator picture redAllocator · Jul 31, 2017

Runs the specified action on the UI thread.

I would like to recommend read this site runOnUiThread

runOnUiThread(new Runnable() {
  @Override
  public void run() {
    // call the invalidate()
  }
});