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