章出自: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. Xilinx 网站资源导

    Xilinx 网站资源导读 ———版权声明———–本文作者 Ricky Suwww.fpganotes.comrickysu.fpga@gmail.com 欢迎转载,转载请保持原样及署名商业使用须得到 ...

  2. 记工作中的git遇到的问题

    看了 git 回退到某版本后,再在此版本上更新,无法push 操作前,我备份了修改了目录,准备建一个分支进行操作 我在本地revert了一次,commit到了远程仓库.然后上个版本的修改给恢复了... ...

  3. J2EE项目应用开发过程中的易错点

    场景一 实体类型与数据库类型不一致,在进行条件查询过程中不走索引 分析 字段值类型和数据库定义的字段类型不一致时,MySQL就会在内部做数据转化, 它的处理行为就会和我们期望的有些不一样,当我们使用整 ...

  4. [转]在SQLServer中实现Sequence的高效方法

    如果在ORACLE里面用惯了Sequence的兄弟们,要在SqlServer里实现Sequence,就会发现没有现成的Sequence对象可以Create了.那应该怎么办呢? 当然这点小问题是难不倒我 ...

  5. 永久设置 NLS_DATE_FORMAT 方法

    在客户端(如WEB服务器),设置环境变量即可,如: NLS_DATE_FORMAT YYYY-MM-DD HH24:MI:SS   在程序处执行会影响后续SQL性能 ALTER SESSION SET ...

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

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

  7. 关于修改tabbar的颜色的问题

    首先,项目是在故事板中搭建的,所以遇到这个问题的时候,首先是想到在故事板中找到相关的属性,确实是有一个Selected Image,但是设置了这个图片以后,运行的效果是,点击选择后,本身的image就 ...

  8. Warning: Attempt to present * on * which is already presenting *

    Warning: Attempt to present (要被presented的控制器)  on (哪个控制器来presenting) which is already presenting (已经 ...

  9. C#常量字段

    const 常量字段使用方法 using System;using System.Collections.Generic;using System.Linq;using System.Text;usi ...

  10. 2017 google Round C APAC Test 题解

    题解参考网上的答案,以及我自己的想法. 主要参考网站:http://codeforces.com/blog/entry/47181,http://codeforces.com/blog/entry/4 ...