Android-java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

时间:2021-07-08 13:21:18
章出自:luchg技术交流 http://www.luchg.com 版权所有。本站文章除注明出处外,皆为作者原创文章,可*引用,但请注明来源,谢谢。 
Android-java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

当我们在一个新的线程中使用android UI时,遇到这个异常

看了很多文章也还是不太清楚具体是什么原因,但是处理这个问题是很简单的,如下代码:

1
2
3
4
5
6
7
8
9
10
11
@Override
public void run() {
         
        //前面代码省略
 
    Looper.prepare();
    Log.i("---my tag---", "下载完成:"+downloadPath);
    Toast.makeText(context, fileName+"下载完成...", Toast.LENGTH_LONG).show();
    Looper.loop();
             
}

在使用Toast之前加一行Looper.prepare();,然后在Toast之后加一行Looper.loop();,即可实现。看了很多文章也没理解,所以这里就不误导大家了,感兴趣的自己搜搜原因。

上面代码是我自己写的文件下载功能一部分代码,完整代码可以查看:

Android-文件下载

文章出自:luchg技术交流 http://www.luchg.com 版权所有。本站文章除注明出处外,皆为作者原创文章,可*引用,但请注明来源,谢谢。 
Android-java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

Android-java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()的更多相关文章

  1. java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()解决办法

    代码改变世界 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.pre ...

  2. java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

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

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

  4. Android 线程更新UI报错 : Can't create handler inside thread that has not called Looper.prepare()

    MainActivity中有一个按钮,绑定了save方法 public void save(View view) { String title = titleText.getText().toStri ...

  5. Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()

    问题: 写了一个sdk给其他人用,提供一个回调函数,函数使用了handler处理消息 // handler监听网络请求,完成后操作回调函数 final Handler trigerGfHandler ...

  6. Android Exception 13(Can't create handler inside thread that has not called Looper.prepare())

    10-12 17:02:55.500: E/AndroidRuntime(28343): FATAL EXCEPTION: Timer-2 10-12 17:02:55.500: E/AndroidR ...

  7. 在子线程中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 ...

  8. 【转】在子线程中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 ...

  9. 转 在子线程中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 ...

随机推荐

  1. httpClient实现微信公众号消息群发

    1.实现功能 向关注了微信公众号的微信用户群发消息.(可以是所有的用户,也可以是提供了微信openid的微信用户集合) 2.基本步骤 前提: 已经有认证的公众号或者测试公众账号 发送消息步骤: 发送一 ...

  2. 改变win7驱动图标

    一.背景 自己做的USB设备是HID设备,注册到设备管理器中就是"HID Compliant device",显得很业余,然后想去改变这个图标和名称,也就有了此篇文章 二.正文 还 ...

  3. 小波说雨燕 第三季 构建 swift UI 之 UI组件集-视图集(六)Picker View视图 学习笔记

    想对PickerView进行操作,只能在代码中操作. 下面 ,再添加三个label组件,然后将所有组件配置到代码中(看代码),然后要实现对PickerView的操作,就要实现它的DataSource协 ...

  4. docker ubuntu

    DOCKER教程 注意事项 1.官方申明docker还是在开发完善中,不建议在运营的产品中使用它,但是现在离正式版越来越接近了,请关注我们的博客http://blog.docker.io/2013/0 ...

  5. 【剑指offer】二叉树中和为某一值的路径

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/26141815 题目描写叙述: 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数 ...

  6. MFC枚举USB设备碰到的一个疑难,还没解决

    代码如下: 打开USB Hub设备之后,返回句柄hHubDevice,然后使用EnumerateHubPorts来枚举Hub的端 口.疑问在代码的中文注释中. bool CUsbEnumHub::En ...

  7. laravel安装 笔记

    http://laod.cn/hosts/2015-google-hosts.html 谷歌FQIP laravel安装和设置流程 1安装composer , VirtualBox和Vagrant 下 ...

  8. masm的一些常用编译选项

    ml命令行选项: /Dsymbol[=value] 定义给定名称的文本宏 /Fl 生成lst文件 /Sn lst文件中关闭符号表 /I 设置include文件的路径 /link 发送给link的连接器 ...

  9. mybatis出现无效的列类型

    package com.webapp.hanqi.test; import java.util.Date; import org.junit.jupiter.api.AfterEach; import ...

  10. Vue——轻松实现vue底部点击加载更多

    前言 需求总是不断改变的,好吧,今天就把vue如何实现逐步加载更多和分布加载更多说下,默认你知道如何去请求数据的哈 一次请求 页面 使用slice来进行限制展现从0,a的数据 <div v-fo ...