問題的報錯位置是我用handler寫了一個定時消息,在handleMessage()方法里直接寫了ui更新操作, 這個報錯的原因一般是因為在子線程中直接操作UI導致的(eg, setText())。因為Android中相關的view和控件操作都不是線程安全的,所以Android禁止在非UI線程更新UI。 ps. 這里所指的操作一般是能會導致控件重繪(invalidate)的操作,在子線程執行setTextColor此類UI操作不會報錯
**那么,如果子線程有需要操作UI的需求怎么辦呢?一般來說,可以通過如下幾種方式來實現。其原理都是通過間接調用,在主線程中去操作UI:
使用View.post方法,post一個Runnable對象,在run方法中操作UI**
final String text = Thread.currentThread().getId() + "_" + i++;
final TextView txtTime1 = (TextView)mainActivity.findViewById(R.id.txtTime1);
txtTime1.post(new Runnable() {@Overridepublic void run() {txtTime1.setText("[View.post]" + text);}
});
使用Activity.runOnUiThread方法,傳入一個Runnable對象,在run方法中操作UI
final TextView txtTime2 = (TextView)mainActivity.findViewById(R.id.txtTime2);
mainActivity.runOnUiThread(new Runnable() {@Overridepublic void run() {txtTime2.setText("[Activity.runOnUiThread]");}
});
在Activity中創建一個Handler對象,在子線程中通過handle.post方法,傳入一個Runnable對象,在run方法中操作UI
final TextView txtTime4 = (TextView)mainActivity.findViewById(R.id.txtTime4);
mainActivity.mHandler.post(new Runnable() {@Overridepublic void run() {txtTime4.setText("[Handler.post]" + text);}
});
在Activity中創建一個Handler對象,并且在handlerMessage方法中針對指定的消息去操作UI,而消息則是有子線程通過handler.sendMessage方法發送 1)在Activity的onCreate方法中創建一個Handler對象,并在收到自定義消息UPDATE_TIME_ID之后操作UI
mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);if (msg.what == UPDATE_TIME_ID) {String strTime = msg.getData().getString(UPDATE_TIME_KEY);((TextView)findViewById(R.id.txtTime3)).setText(strTime);} }
};
2) 在子線程中通過handler.sendMessage方法發送ID為UPDATE_TIME_ID的消息
Message updateTimeMessage = Message.obtain();
Bundle bundle = new Bundle();
bundle.putString(UPDATE_TIME_KEY, "[Handle.sendMessage]" + text);
updateTimeMessage.what = MainActivity.UPDATE_TIME_ID;
updateTimeMessage.setData(bundle);
mainActivity.mHandler.sendMessage(updateTimeMessage);
總結
以上是生活随笔 為你收集整理的android报错:Only the original thread that created a view hierarchy can touch its views. 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。