章出自:
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()
当我们在一个新的线程中使用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();,即可实现。看了很多文章也没理解,所以这里就不误导大家了,感兴趣的自己搜搜原因。
上面代码是我自己写的文件下载功能一部分代码,完整代码可以查看:
文章出自:
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()