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 ...
随机推荐
- WCF编程系列(四)配置文件
WCF编程系列(四)配置文件 .NET应用程序的配置文件 前述示例中Host项目中的App.config以及Client项目中的App.config称为应用程序配置文件,通过该文件配置可控制程序的 ...
- 怎样向IT行业的朋友说明《圣经》的重要性
“世界的官方文档”——怎么样?
- ###STL学习--vector
点击查看Evernote原文. #@author: gr #@date: 2014-08-11 #@email: forgerui@gmail.com vector的相关问题.<stl学习> ...
- java.util.AbstractStringBuilder源码分析
AbstractStringBuilder是一个抽象类,是StringBuilder和StringBuffer的父类,分析它的源码对StringBuilder和StringBuffer代码的理解有很大 ...
- BootstrapDialog点击空白处禁止关闭
在乐学一百的项目当中引用到了BootstrapDialog,其中后台发送短信时,为了防止管理员编辑了半天的短息,突然间因为点击某个空白区域导致丢失,所以在此禁用掉点击空白关闭弹出框. 主要属性为: c ...
- Html学习_style属性应用
用style属性改变字体大小.颜色.字体.背景颜色和对齐方式 <!DOCTYPE html> <html> <head> <title>Putting ...
- WPF中三种方法得到当前屏幕的宽和高
WPF程序中的单位是与设备无关的单位,每个单位是1/96英寸,如果电脑的DPI设置为96(每个英寸96个像素),那么此时每个WPF单位对应一个像素,不过如果电脑的DPI设备为120(每个英寸120个像 ...
- C语言数据结构之栈:括号匹配
括号匹配这是个很简单的题目,如果只有小括号,就模拟进栈和出栈的过程就行了: 注:输入时'@'作为结束标志 #include <stdio.h> int main() { freopen(& ...
- OpenGL列主元矩阵的运算
大学线性代数课本是用的行主元矩阵,OpenGL使用的是列主元矩阵.有什么差别呢,对于线性代数学得不够扎实的孩纸来说,还挺烦的. 其实明白了就觉得很简单: 从课本的角度来看,平移矩阵是诸如这样的: 平移 ...
- MVC5添加控制器总是报“Multiple object sets per type are not supported”
http://www.asp.net/mvc/tutorials/mvc-5/introduction/creating-a-connection-string 按照上面的指导做练习, 总报小面的错 ...