android handler looper thread
在线程中调用包含创建handler方法的时候,会报错,提示:
“need call Looper.prepare()” -- 在创建之前,调用Looper.prepare()方法来创建一个looper
但是这个包含创建handler的方法,可能在主线程中调用,也可能在子线程中调用。
在主线程中调用的时候,你给它加上looper.prepare()方法,它可能会报错,提示:
“Only one Looper may be created per thread” -- 每个线程之中,只能有一个Looper
解决的办法:
if (Looper.myLooper() == null) {
Looper.prepare();
}
其中,Looper.myLooper()方法的返回值是当前线程的Looper对象,返回的是Null的时候,则需要通过Looper.prepare()方法创建Looper
可以对照Looper类的源码查看一下,即可。
android handler looper thread的更多相关文章
- android Handler、Thread和Runnable
android里面的创建的Handler对象并不是新建一个新的线程,而是在主线程执行,主线程的消息队列中循环. java中实现一个线程有两种方法,一种是继承Thread类,一种是实现Runnable接 ...
- android handler looper
http://www.cnblogs.com/plokmju/p/android_Handler.html
- 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进阶(十六)子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误
原子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误 今天用子线程调Toast报 ...
- 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 ...
- Android进阶(八)Can't create handler inside thread that has not called Looper.prepare()
Error:Can't create handler inside thread that has not called Looper.prepare() 原代码: //利用Handler消息传递机制 ...
- Android 错误提示: Can't create handler inside thread that has not called Looper.prepare()
Can't create handler inside thread that has not called Looper.prepare() 将 Handler handler = new Hand ...
- Android之消息机制Handler,Looper,Message解析
PS:由于感冒原因,本篇写的有点没有主干,大家凑合看吧.. 学习内容: 1.MessageQueue,Looper,MessageQueue的作用. 2.子线程向主线程中发送消息 3.主线程向子线程中 ...
随机推荐
- Java多线程之synchronized(四)
前面几章都是在说synchronized用于对象锁,无论是修饰方法也好修饰代码块也好,然而关键字synchronized还可以应用到static静态方法上,如果这样写,那就是对当前的*.java文件所 ...
- (" use strict")Javascript 严格模式详解
Javascript 严格模式详解 转载别人的博客内容,浏览了一遍,没有全部吸收,先保存一下链接 http://www.ruanyifeng.com/blog/2013/01/javascript_s ...
- 复制virtualenv环境到其他服务器环境配置的方法
要在n多服务器端部署python的应用,虽然python本身是跨平台的,当时好多第三方的扩展却不一定都能做到各个版本兼容,即便是都是linux,在redhat系列和ubuntu系列之间来回导也是个很让 ...
- Django forms 关于select和checkbox设置初始选中值及让前端选中指定值
Django的forms和models一样很牛逼.他有两种功能,一是生成form表单,还有就是form表单的验证. 这里主要说一下生成form表单时经常用到的需要设置 初始值 / 默认值 的情况. 1 ...
- shell学习之字符串处理
1.获取字符串长度 value=abcd;echo ${#value} 输出:42.获取子串 value=abcd;:} 输出:d value:a:b,其中a表示起始位置,b表示获取的子串的长度. 3 ...
- [Android]Dalvik的BOOTCLASSPATH和dexopt流程
BOOTCLASSPATH简介1.BOOTCLASSPATH是Android Linux的一个环境变量,可以在adb shell下用$BOOTCLASSPATH看到.2.BOOTCLASSPATH于/ ...
- hibernate 数据关联多对多 4.1
多对多,必须有一张关系表来维持关系 数据库student,teacher student_teacher 三张表 但是在pojo中只需要建立student和teacher两个类,除非关系表也代表某种业 ...
- GridView用法的修改和删除
(前台) <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="Fa ...
- UI常用控件的一些属性
UILable 1 //设置文本信息 2 nameLable.text = @"用户名:"; 3 //对齐方式(居中 居左 局右); 4 nameLable.textAlignme ...
- cocos2d-x3.6 连连看连通画线
我的博客:http://blog.csdn.net/dawn_moon 网上看到非常多人写的连连看,都没有画连线的实现.事实上要话连线挺简单的.cocos2d-x 提供了一个非常方便的绘图形的类.Dr ...