分析:callback中回调的response方法中还是在子线程中运行的,所以要调取Toast必须回到主线程中更新ui

解决方法:在调用Toast(或者AlertDialog)的地方的前面加上Looper.prepare(),后边加上Looper.loop()即可解决问题;也就是说用Looper.prepare()和Looper.loop()把Toast前后包起来。

prepare方法是在子线程中new Looper,把Toast放进了队列之中,loop方法就会进入无限循环取值。

还一个方法就是开启一个onUiThread 或者异步消息handle 或者asyncTask

转载于:https://my.oschina.net/sully2bynnee/blog/865736

在okhttp的callback回调中加Toast出现Cant create handler inside hread that has not called Looper.prepare()...的更多相关文章

  1. OkHttp下载文件中途断网报Can't create handler inside thread that has not called Looper.prepare()异常的解决办法

    最近做项目时出现个问题. 在一个基类中,创建一个Handler对象用于主线程向子线程发送数据,代码如下: this.mThirdHandler = new Handler(){ @Override p ...

  2. Android进阶(十六)子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误

    原子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误 今天用子线程调Toast报 ...

  3. 关于子线程使用Toast报错Can't create handler inside thread that has not called Looper.prepare()的解决办法

    形同如下代码,在Thread中调用Toast显示错误信息: new Thread(new Runnable(){ @Override public void run() { try{ weatherD ...

  4. 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()

    在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException:  Can't create handler inside thread that has ...

  5. 【转】在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()

    在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException:  Can't create handler inside thread that has ...

  6. 转 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()

    在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException:  Can't create handler inside thread that has ...

  7. 工具类ToastUtil 避免在子线程中使用抛异常 "Can't create handler inside thread that has not called Looper.prepare()"

    package com.example.kbr.utils; import android.view.Gravity; import android.widget.Toast; import io.r ...

  8. android Toast提示异常:java.lang.RuntimeException: Can't create handler inside thread that has not called

    Toast只能在UI线程弹出,解决此问题可以在Toast前后加两行代码,如下所示: Looper.prepare(); Toast.makeText(getApplicationContext(),& ...

  9. Android开发之在子线程中使用Toast

    在子线程中使用Toast的时候,出现Force close. 错误提示:Can't create handler inside thread that has not called Looper.pr ...

随机推荐

  1. VMware中虚拟机克隆后多台主机网络冲突

    在Vmware中将虚拟机的一台centos7机器克隆了3台,然后启动机器后出现机器都能够上网,但是无法在本地xshell中同时进行连接,且连接的某一台虚拟机都会经常中断 虚拟机克隆后,会遇到的问题: ...

  2. Python循环语句,对象

    Python循环语句,对象 案例1:斐波那契数列 案例2:模拟cp操作 案例3:生成8位随机密码 1 案例1:斐波那契数列 1.1 问题 编写fib.py脚本,主要要求如下: 输出具有10个数字的斐波 ...

  3. 配置HTTPS网站服务器

                                     配置HTTPS网站服务器    案例1:配置安全Web服务 1.1问题 本例要求为站点http://server0.example.c ...

  4. Linux 下如何隐藏自己不被发现?

    可能在某些情况下,自己运行的程序不想或者不方便被其他人看到,就需要隐藏运行的进程.或者某些攻击者采用了本文介绍的隐藏技术,也可以让大家看到如何进行对抗. 隐藏有两种方法: kernel 层面,不对用户 ...

  5. 10.2 io流 之字节流和字符流

    FileWriter 用于写入字符流.要写入原始字节流,请考虑使用 FileOutputStream. io流相关文档: https://www.cnblogs.com/albertrui/p/836 ...

  6. lr使用soap协议,来对webservice接口进行测试

    实际项目中基于WSDL来测试WebService的情况并不多,WSDL并不是WebService测试的最佳选择. 最主要的原因还是因为WSDL文档过于复杂. 在案例(天气预报WebService服务) ...

  7. shell命令-if语句

    判断参数的个数 -ne 不等于 -eq 等于 -gt 大于 -lt 小于 -ge 大于等于 -le 小于等于 if [ "$#" -ne 1 ];then echo "n ...

  8. Flex Socket与Java通信实例说明(转)

    Flex Socket与Java通信实例说明(转) 这两天一直在flex的Socket ,现在终于懂了很多.由浅到深一步一步深入.慢慢体会实例,虽然实例都是在网上找的,但也经过了我的测试.我比较喜欢注 ...

  9. D - Catch That Cow BFS

    农夫知道一头牛的位置,想要抓住它.农夫和牛都于数轴上 ,农夫起始位于点 N(0<=N<=100000) ,牛位于点 K(0<=K<=100000) .农夫有两种移动方式: 1. ...

  10. Windows安装Tesseract-OCR 4.00并配置环境变量

    一.前言 Tesseract-OCR 是一款由HP实验室开发由Google维护的开源OCR(Optical Character Recognition , 光学字符识别)引擎.与Microsoft O ...