章出自: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项目版本较低,check out 下来后报错

    check out下来后报错提示: svn: E155036: Please see the 'svn upgrade' commandsvn: E155036: Working copy '/hom ...

  2. Ubuntu 修改时间

    输入"date",显示的是: Tue Jan :: UTC 输入"date -R" 显示的是: Tue, Jan :: + 和北京时间相差了8个小时. 1.选择 ...

  3. CSS属性[text-overflow]使用问题

    text-overflow:clip | ellipsis 这个属性使用必须通过几个属性一块才能使用 1,overflow:hidden; 这个属性是内容区装不下内容应该怎么办.这里让溢出内容直接不显 ...

  4. alert

    先别着急测试,来猜测一下下面一行代码执行的结果 alert(alert(1234567)); 此刻,我自己还不是不太理解 我的分析是这样: alert() 是window下面的一个方法 alert(1 ...

  5. mysql查找重复

    중복된 것 모두 찾기    SELECT 필드명, count(*) FROM 테이블명  GROUP BY 필드명   mysql> SELECT t1, count(*) FROM tes ...

  6. Java——String.split()函数

    在java doc里有 String[] java.lang.String.split(String regex) Splits this string around matches of the g ...

  7. [转]Windows Shell 编程 第二章 【来源:http://blog.csdn.net/wangqiulin123456/article/details/7987893】

    第二章Shell的结构  “Shell 编程”的大伞之下有大量的API函数和COM接口.这个种类繁多的‘命令’集允许你用不同的方法对Windows Shell进行编程.函数和接口并不是两种提供相同功能 ...

  8. Web应用程序安全必须重视八大问题

    摘自:http://netsecurity.51cto.com/art/201402/428709.htm 对于任何一个项目,开始阶段对于交付安全的应用来说非常关键.适当的安全要求会导致正确的安全设计 ...

  9. mysql远程访问的权限

    1.改表法 mysql>use mysql; mysql>update user set host = '%' where user = 'root'; 2.授权法 例如,你想myuser ...

  10. 简单改造 starling 中的 AssetManager 让其更适合 批次加载纹理

    API文档参考:http://doc.starling-framework.org/core/starling/utils/AssetManager.html 项目想以不改动starling的情况下对 ...