日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android报错:Only the original thread that created a view hierarchy can touch its views.

發布時間:2024/1/8 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android报错:Only the original thread that created a view hierarchy can touch its views. 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題的報錯位置是我用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.的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。