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()
当我们在一个新的线程中使用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-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()的更多相关文章
- 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 ...
- java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
- android Toast提示异常:java.lang.RuntimeException: Can't create handler inside thread that has not called
Toast只能在UI线程弹出,解决此问题可以在Toast前后加两行代码,如下所示: Looper.prepare(); Toast.makeText(getApplicationContext(),& ...
- 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 ...
- Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()
问题: 写了一个sdk给其他人用,提供一个回调函数,函数使用了handler处理消息 // handler监听网络请求,完成后操作回调函数 final Handler trigerGfHandler ...
- 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 ...
- 在子线程中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 ...
- 【转】在子线程中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 ...
- 转 在子线程中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 ...
随机推荐
- LintCode 55 比较字符串
比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母 注意事项 在 A 中出现的 B 字符串里的字符不需要连续或者有序. 样例 给出 A = "ABC ...
- jQuery 判断div是否shown
// Checks for display:[none|block], ignores visible:[true|false] $(element).is(":visible") ...
- css 去除input 获取焦点的蓝色边框
input{ outline:0px; }
- 3.x vector的用法
#include<vector> //struct struct GOLD_STRUCT { Sprite * goldspSprite; int goldValue; ...
- 【转】Android自动化测试之MonkeyRunner录制和回放脚本(四)
测试脚本录制: 方案一: 我们先看看以下monkeyrecoder.py脚本: #Usage: monkeyrunner recorder.py #recorder.py http://mirror ...
- 第一个Cookie应用
Cookie应用:显示用户上次访问时间 package com.itheima.cookie; import java.io.IOException; import java.io.PrintWrit ...
- scala学习笔记:理解并行集合par
scala> (1 to 5).foreach(println(_)) 1 2 3 4 5 scala> (1 to 5).par.foreach(println(_)) 3 1 4 2 ...
- java星座、年龄、日期等
星座: public static String getStar(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(dat ...
- Css颜色定义的方法汇总color属性设置方式
颜色的定义方式用rgb()里面带上十进制的数字来定义. color:rgb(211,123,135); 用预定义的颜色名称. color:red; rgba()最后一个参数是不透明度. color:r ...
- Gulp实战(二)
一.配置环境 1.基于NodeJs安装Git,npm,gulp 2.安装各类插件 3.参考文档 http://www.tuicool.com/articles/UbaqyyJ http://www.t ...