分析: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. Java基础知识3-类和对象(1)

    面向过程和面向对象的区别 面向过程(结构化程序设计) 实际上是一个面向操作过程,首先设计一系列过程(算法)来求解问题(操作数据),然后再考虑存储数据的方式(组织数据).即程序=算法\+数据结构.对应典 ...

  2. Docket 容器引擎

    Docker 是世界领先的软件容器平台.是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中, 然后发布到任何流行的Linux或Windows机器上,可以实现虚拟化(软件 ...

  3. Shell:Day02.笔记

    重定向和管道符:1.重定向 程序 = 指令 + 数据        命令   变量  在程序中,数据如何输入?有如何输出?  数据输入:键盘 -- 标准输入,但是并不是唯一输入方式:    --std ...

  4. postman 参数传递

    pm.test("Status code is 200", function () { pm.response.to.have.status(200); }); pm.test(& ...

  5. 怎样让scratch里的人物两腿走动

    需要人物角色至少有两个“造型”,表现走路时的两个动作.以默认的“小猫”觉色为例,它有两个“造型”,可以用来表现奔跑的动作. 但是要想让小猫跑起来,需要脚本来实现,简单跑动脚本如下 scratch学习视 ...

  6. MTK Android修改System分区

    Z:\rk3326_p_hq_rf8637sa\device\rockchip\common\BoardConfig.mk #Calculate partition size from paramet ...

  7. C语言 文件操作(八)

    1.删除文件或目录 int remove(char * filename); [参数]filename为要删除的文件名,可以为一目录.如果参数filename 为一文件,则调用unlink()处理:若 ...

  8. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(八)之Polymorphism

    Polymorphism is the third essential feature of an object-oriented programming language,after data ab ...

  9. 使用 Chrome 插件 Vimium 打造黑客浏览器

    之前一直用 cVim,与 Vimium 功能类似,但是之后不在更新了,故转战到 Vimium. 简介 官网:http://vimium.github.io/ Vimium 是 Google Chrom ...

  10. C++养成好的代码习惯

    [C++小技巧] -------------------------------------------------------------#ifdef _DEBUG    imwrite(" ...