1. 程式人生 > 其它 >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執行緒或主執行緒進行view相關操作,如果想要在view程序要在子執行緒之後進行,就需要阻塞主執行緒。

解決方法:使用Handler物件。子執行緒結束後傳送資訊

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
            ........
            ........
            ........
                Message msg 
= new Message(); msg.what = 1; handler.sendMessage(msg); } }); thread1.start(); } private Handler handler = new Handler(){ public void handleMessage(Message msg) { //一些work執行緒不能處理的UI邏輯....例如 自動點選view iv_search.performClick(); } };

參考:

解決Only the original thread that created a view hierarchy can touch its views_qq_41072388的部落格-CSDN部落格

解決Onlytheoriginalthreadthatcreatedaviewhierarc_Jobinqu_新浪部落格 (sina.com.cn)

關於android中Handler物件的講解_恆安軟體-CSDN部落格