-1

We can see this error when we try to change the Ui tookit in Thread not Ui thread.

W/System.err: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
W/System.err:     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7334)

I just wondering about standard of Ui toolkit.

I thought that every view is a Ui tookit. But I set text to a TextView in another thread, the error was not occurred. (The TextView is not appeared in the layout of MainActivity)

So, is it right the error is occured that the Ui component is changed in layout of current Activity?

hata
  • 11,633
  • 6
  • 46
  • 69
장원준
  • 19
  • 2

1 Answers1

0

I think you know why Google call "UI Thread". Right? This is a paragraph from Google doc:

When an application is launched, the system creates a thread of execution for the application, called "main." This thread is very important because it is in charge of dispatching events to the appropriate user interface widgets, including drawing events. It is also almost always the thread in which your application interacts with components from the Android UI toolkit (components from the android.widget and android.view packages). As such, the main thread is also sometimes called the UI thread.

You can read more in here: https://developer.android.com/guide/components/processes-and-threads#Threads

So for your question, UI Component doesn't need to be change in current Activity.

It should be changed in MAIN THREAD.

And If you read the Google's document more careful. You can see that Google doesn't recommend to change or modify views from other threads.(not UI Thread).

  1. Do not block the UI thread

  2. Do not access the Android UI toolkit from outside the UI thread

NhatVM
  • 1,964
  • 17
  • 25
  • I have one more question. I successed to setText method in another Thread not main Thread in my MainActivity. But the Textview wasn't in layout of MainActivity So users can't see the TextView. it just was in another layout. How could i do that??? – 장원준 Apr 21 '21 at 04:19
  • I'm sorry. I don't get your point. Can you explain it more detail? Or show me your code – NhatVM Apr 22 '21 at 02:15