分析: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. SSM集合定时任务

    1.首先要在spring-config.xml里面配置一下: xmlns:task="http://www.springframework.org/schema/task" htt ...

  2. 用人话告诉小白:什么是项目管理(例如Maven),什么是调试工具(即debugger),什么是编译(即compile)

    项目管理 以java程序的项目管理软件Maven为例,java程序根据代码的不同需要不同的jar文件才能编译运行. 人物:两个程序员A和B 物品:一个java程序G,许多jar文件 场景:当A在自己电 ...

  3. .NET项目升级手记:可为空引用

    c# 8引入了新特性:"可为空引用"(详情),这个功能个人觉得挺好的,能够非常明确的表现程序设计者的意图,编译器能够进行检查,尽最大可能减小NullReferenceExcepti ...

  4. Go gRPC教程-服务端流式RPC(三)

    前言 上一篇介绍了简单模式RPC,当数据量大或者需要不断传输数据时候,我们应该使用流式RPC,它允许我们边处理边传输数据.本篇先介绍服务端流式RPC. 服务端流式RPC:客户端发送请求到服务器,拿到一 ...

  5. Vulnhub DC-5靶机渗透

    信息搜集 老样子,先找到靶机IP和扫描靶机 nmap -sP 192.168.146.0/24 #找靶机ip nmap -sS -Pn -A 192.168.146.141 #扫描端口 这次开的是80 ...

  6. Django模拟ASP.NET MVC 自动匹配路由(转载)

    项目结构 操作步骤 1.创建项目结构如上图 2.在myapp目录下创建urls文件,代码: from django.conf.urls import patterns, url from untitl ...

  7. Jackson优化使用实例

    Jackson优化使用实例 博客分类: Java综合   JSON的三种处理方式  Jackson提供了三种可选的JSON处理方法(一种方式及其两个变型): 流式 API:(也称为"增量分析 ...

  8. 第一天 简单的python认证登陆代码

    #!/usr/bin/env python3# -*- coding:utf-8 -*-# name:zzyu welcome = '''-----------welcome to home----- ...

  9. Angular input / ion-input ion-searchbar 实现软件盘换行 改 搜索 并且触发搜索方法 Android iOS适用

    Angular 实现软件盘 换行 改 搜索 并且除非 搜索方法:    Form 必须有 action="javascript: return true;”   input / ion-in ...

  10. BFC的理解与应用

    什么是BFC(Block formatting contexts) BFC的通俗理解: 首先BFC是一个名词,是一个独立的布局环境,我们可以理解为一个箱子(实际上是看不见摸不着的),箱子里面物品的摆放 ...