Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()
问题:
写了一个sdk给其他人用,提供一个回调函数,函数使用了handler处理消息
// handler监听网络请求,完成后操作回调函数
final Handler trigerGfHandler = new Handler() {
public void handleMessage(Message msg) {
listener.onGeofenceTrigger(gfMatchIds);
}
};
在使用这个sdk提供的函数时,报错:
01-02 15:46:10.498: E/AndroidRuntime(16352): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
使用方式是在service中使用。在activity中使用正常。
问题解决:
在调用handler的方法前执行Looper.prepare()。Looper用于封装了android线程中的消息循环,默认情况下一个线程是不存在消息循环(message loop)的,需要调用Looper.prepare()来给线程创建一个消息循环,调用Looper.loop()来使消息循环起作用。
因为activity的主线程是自己创建消息队列的,所以在Activity中新建Handler时,不需要先调用Looper.prepare()
代码:
Looper.prepare();
// handler监听网络请求,完成后操作回调函数
final Handler trigerGfHandler = new Handler() {
public void handleMessage(Message msg) {
listener.onGeofenceTrigger(gfMatchIds);
}
};
Looper.loop();
参考:http://www.myexception.cn/mobile/410671.html
========================
新问题:
当此函数在activity中调用时,也会报错,因为activity已经自动创建了消息队列。所以重复创建会出错。
查看官网关于Looper的例子:
class LooperThread extends Thread {
public Handler mHandler;
public void run() {
Looper.prepare();
mHandler = new Handler() {
public void handleMessage(Message msg) {
// process incoming messages here
}
};
Looper.loop();
}
}
将handler写在一个线程中。使此消息队列与主线程中消息队列分离。
最终写法为:
class LooperThread extends Thread {
public void run() {
Looper.prepare();
trigerGfHandler = new Handler() {
public void handleMessage(Message msg) {
// process incoming messages here
listener.onGeofenceTrigger(gfMatchIds);
}
};
Looper.loop();
}
}
LooperThread looper = new LooperThread();
looper.start();
Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()的更多相关文章
- 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 ...
- 在子线程中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 ...
- 【转】在子线程中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 ...
- 转 在子线程中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 ...
- 关于子线程使用Toast报错Can't create handler inside thread that has not called Looper.prepare()的解决办法
形同如下代码,在Thread中调用Toast显示错误信息: new Thread(new Runnable(){ @Override public void run() { try{ weatherD ...
- 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报 ...
- OkHttp下载文件中途断网报Can't create handler inside thread that has not called Looper.prepare()异常的解决办法
最近做项目时出现个问题. 在一个基类中,创建一个Handler对象用于主线程向子线程发送数据,代码如下: this.mThirdHandler = new Handler(){ @Override p ...
- 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 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 ...
随机推荐
- JavaScript高级程序设计(八):基本概念--操作符
操作符包括:算术操作符.位操作符.关系操作符和相等操作符. 一元操作符 1.只能操作一个值得操作符,即递增和递减操作符: 2.递增(++)和递减(--)操作符包括:前置型和后置型.前置型位于要操作的变 ...
- [leetcode]最长递增序列
class Solution { public: int lengthOfLIS(vector<int>& nums) { int n=nums.size(); ) ; vecto ...
- C语言经典案例
题目:企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%:20万到40万 ...
- hdu 5009 Paint Pearls
首先把具有相同颜色的点缩成一个点,即数据离散化. 然后使用dp[i]表示涂满前i个点的最小代价.对于第i+1个点,有两种情况: 1)自己单独涂,即dp[i+1] = dp[i] + 1 2)从第k个节 ...
- 获取input标签的所有属性
1.用jquery$("input[name='btnAdd']").attr("value") 获取value属性值,其它属性换attr的参数就OK 例1: ...
- 正确理解javascript的this关键字
javascript有this关键字,它和javascript的执行上下文有着密切的关系,就是说this具体指代什么要根据它的上下文来判断. 一.this和对象的关系 var Person={ ...
- 路由器无线桥接 router wireless bridge
实验环境:TP-Link A,TP-Link B,两个路由器都有子网,分别为子网 A,子网 B.TP-Link A连接学校子网 IP A,TP-Link B连接学校子网 IP B.两个路由器都能够通过 ...
- 使用vagrant作为开发环境后,js报错
当你尝试修改一个js,并且用同样的方法更新之后,会遇到类似的问题,是的,就算重启VM上任何服务,甚至重启VM,依旧没有用,当然,比起其他资源文件,浏览器的反应会强烈一些,因为浏览器会提示未知错误,而你 ...
- less的使用方法
LESS学习 1.变量 我们可以把一个css样式的值赋给一个参数,然后再设置样式的时候只需要设置这个参数名,如果要修改,就改参数的值就可以了. demo.html <!DOCTYPE html& ...
- MSSql ID自动增长删除数据重1开始
dbcc checkident('db_Tome1.dbo.员工信息表',reseed,0) 注:dbcc checkident('表名',reseed,0)