章出自: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. svn has stoped working

    各种问各种搜  ,看来还得靠自己,,,,下面是解决方案; 如果你出现这种情况,  右键点击svn的选项,就弹出这个框框,,,,点击svn里的哪个选项都是弹出这个框框,,,,重新安装,重启,换各种版本安 ...

  2. HTML——<meta http-equiv="content-type" content="text/html; charset=UTF-8">

    没有添加这句话的编码方式的话,很容易就乱码了 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&g ...

  3. System.Web.Mvc.Html 命名空间小计(转)

    最近在看MVC框架,发现这个博文对初学者可能有帮助,故转之. 1,Html.Action    使用指定参数调用指定子操作方法并以 HTML 字符串形式返回结果. Html.Action() < ...

  4. ### 线性回归(Regression)

    linear regression logistic regression softmax regression #@author: gr #@date: 2014-01-21 #@email: fo ...

  5. ###《Effective STL》--Chapter2

    点击查看Evernote原文. #@author: gr #@date: 2014-09-15 #@email: forgerui@gmail.com Chapter2 vector和string T ...

  6. 第21条:理解Objective-C错误模型

    首先要注意的是: “自动引用计数”(Automatic Reference Counting, ARC,参见第30条)在默认情况下不是“异常安全的”(exception safe).具体来说,这意味着 ...

  7. Container容器控件的使用、Hbox与Vbox布局管理器的使用、以及AjaxAction前后台事件响应

    1.由于有前后台交互功能,需要在Spring上下文中注册一个用于提供服务的bean,对于这个bean使用Spring提供的@Component标注,如果需要使用@Component注解,需要在项目中W ...

  8. CUDA_矢量相加

    #include<iostream> #define N 10 _ _global_ _ void add(*a,*b,*c) { int tid=blockIdx.x; if(tid&l ...

  9. (poj)1502 MPI Maelstrom

    题目链接:http://poj.org/problem?id=1502 Description BIT has recently taken delivery of their processor A ...

  10. asp.net mvc上传头像加剪裁功能介绍

    正好项目用到上传+剪裁功能,发上来便于以后使用. 我不能告诉你们其实是从博客园扒的前台代码,哈哈. 前端是jquery+fineuploader+jquery.Jcrop 后台是asp.net mvc ...