3/08/2018

[CODING] Android "Only the original thread that created a view hierarchy can touch its views." 에러 해결방법

안드로이드의 UI를 변경할 때 간혹 이런 에러가 발생합니다.

Code
public void changeTextView(String ttext){
 textView.setText(text);
}

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

원인은 사용자 인터페이스와 관련된 모든 동작은 onCreate () 및 이벤트 처리가 실행되는 주 스레드 또는 UI 스레드에서 수행되어야 하는데, 다른 스레드에서 동작할 때 이런 에러가 발생합니다. runOnUiThread를 사용하거나 Handlers를 사용해서 해결이 가능합니다.

public void changeTextView(String ttext){
 runOnUiThread(new Runnable() {
  public void run() {
   textView.setText(text);
  }
 });
}

Reference

https://stackoverflow.com/questions/5161951/android-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-vi



HAHWUL

Security engineer, Gopher and H4cker!

Share: | Coffee Me:

0 개의 댓글:

Post a Comment