기본 콘텐츠로 건너뛰기

Android, UI 갱신, Error : Only the original thread that created...

UI Thread 이외에서 UI를 갱신하려고 하면 CalledFromWrongThreadException:Only the original thread that created a view hierarchy can touch its views. 라는 문구를 보게 된다.

Handler

UI의 권한이 있는 Thread의 핸들러에 메시지를 보낸다.
Thread + Handler 조합으로 연속적인 갱신이 가능.


public final Handler handler = new Handler{
@Override
public void handleMessage(Message msg) {
//UI 갱신
}
}


외부에서
xxx.handler.sendEmptyMessage();


Activity.runOnUiThread

UI 갱신 권한이 있는 스레드의 인스턴스를 얻을수 있다면


AActivity.this.runOnUiThread(
new Runnable(){
public void run(){
AActivity.this.xxxx.setText("xxxx");
}}
);



Handler handler = new Handler();

...

handler.post(
new Runnable(
public void run(){
//UI 갱신
}
)
);




View.post(Runnable)
View.postDelayed(Runnable, long)

저작자 표시

댓글