分析: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. MFC之TreeCtrl遍历所有节点

    这个例子,主要是查找树上的某个节点,并展开选中它.采用来了递归方法来实现.主要用到的方法: CTreeCtrl::GetChildItemHTREEITEM GetChildItem( HTREEIT ...

  2. Java第三十四天,IO操作(续集),非基本对象的读写——序列化流

    一.序列化与反序列化 以前在对文件的操作过程当中,读写的对象都是最基本的数据类型,即非引用数据类型.那么如果我们对饮用数据类型(即对象类型)数据进行读写时,应该如何做呢?这就用到了序列化与反序列化. ...

  3. C++值多态:传统多态与类型擦除之间

    引言 我有一个显示屏模块: 模块上有一个128*64的单色显示屏,一个单片机(B)控制它显示的内容.单片机的I²C总线通过四边上的排针排母连接到其他单片机(A)上,A给B发送指令,B绘图. B可以向屏 ...

  4. BaseRuntimeException自定义业务异常类实现

    public class BaseRuntimeException extends RuntimeException { private final int code; public BaseRunt ...

  5. java消除 list重复值及交集,并集,差集

    消除 list重复值 Java代码  public void removeDuplicate(List list) { HashSet h = new HashSet(list); list.clea ...

  6. Redis之ziplist源码分析

    一.ziplist简介 从上一篇分析我们知道quicklist的底层存储使用了ziplist(压缩列表),由于压缩列表本身也有不少内容,所以重新开了一篇,在正式源码之前,还是先看下ziplist的特点 ...

  7. 上班无聊,自己用python做个小游戏来打发时间

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取t.cn ...

  8. 哈密顿绕行世界问题 HDU2181

    题目大意都比较简单,用vector存一下图,然后爆搜就可以了. #include<bits/stdc++.h> using namespace std; ; vector<]; bo ...

  9. Problem C Careful Ascent

    数学问题. 在不经过shield时,竖直速度是1.所以时间就是y/1=y,,,,在经过shield时,时间为shield的数值长度*影响因素,然后总时间把他们加起来,最后再用水平方向的长度除以总时间, ...

  10. 全平台阅读器 StartReader

    前段时间在网上闲逛, 发现了一款全平台阅读器 StartReader, 用了一阵子感觉还不错,网址是: https://www.startreader.com/ 感觉这款阅读器是程序员的福音,it人员 ...