安卓Only the original thread that created a view hierarchy can touch its views
今天在開發安卓的過程中遇到了一個問題,可能也是新手問題吧,就是Only the original thread that created a view hierarchy can touch its views,實際上就是不能在子執行緒中對UI進行更新操作,但是我又需要在處理完某個事務後對UI進行更新,那怎麼辦呢,於是就用到了執行緒通訊。
1、首先設定一個標誌符號
private static final int COMPLETED = 0;
2、設定傳送事務資訊方式
Message message = new Message(); message.what = COMPLETED; handler.sendMessage(message);
3、設定通訊處理機制,就可以把UI更改放在裡面
private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { if (msg.what == COMPLETED) { imageView.setImageResource(R.drawable.finger); //UI更改操作 } } };
就可以完成事務處理啦~