Android安卓 Can't create handler inside thread Thread[Thread-2,5,main] that has not called Looper.prepare()


  • 錯誤復現

在使用okhttp的時候新建了一個thread,在回調函數中創建了handler准備更新ui

  • 錯誤原因

不能在子線程更新ui,需要回到主線程

  • 解決辦法

使用new Handler(Looper.getMainLooper());

  • 代碼樣例
Handler handler = new Handler(Looper.getMainLooper());
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            //更新ui操作
                        }
                    });


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM