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().toString();
String timelength = lengthText.getText().toString();
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(new NewsService(getApplicationContext(),title,timelength));
}
NewsService代码:
@Override
public void run() {
String path = "http://192.168.0.102:8080/videonews/ManageServlet";
Map<String,String> params = new HashMap<String,String>();
params.put("title",title);
params.put("timelength",timelength);
boolean result=false;
try {
result = sendGETRequest(path,params);
} catch (Exception e) {
e.printStackTrace();
}
if(result) {
Toast.makeText(context, R.string.success, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context,R.string.error,Toast.LENGTH_LONG).show();
}
}
报错:
04-18 13:06:36.191 2284-2305/test.example.com.newsmanage E/AndroidRuntime﹕ FATAL EXCEPTION: pool-1-thread-1
Process: test.example.com.newsmanage, PID: 2284
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.widget.Toast$TN.<init>(Toast.java:336)
at android.widget.Toast.<init>(Toast.java:100)
at android.widget.Toast.makeText(Toast.java:250)
at android.widget.Toast.makeText(Toast.java:277)
at test.example.com.service.NewsService.run(NewsService.java:86)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
查了资料,说是Android中不能在子线程中来刷新UI。如果要实现你这功能的话。建议是在你的子线程中添加hander来发送消息更新线程。
下面这样做就OK了
1.在Activity中增加如下代码
private Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0x00 :
Toast.makeText(getApplicationContext(),"save success",Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getApplicationContext(),"save fail",Toast.LENGTH_SHORT).show();
}
}
};
2.启动线程时,将handler传入:
exec.execute(new NewsService(getApplicationContext(),myHandler,title,timelength));
3.在线程中,发送消息给handler:
@Override
public void run() {
String path = "http://192.168.0.102:8080/videonews/ManageServlet";
Map<String,String> params = new HashMap<String,String>();
params.put("title",title);
params.put("timelength",timelength);
boolean result=false;
try {
result = sendGETRequest(path,params);
} catch (Exception e) {
e.printStackTrace();
}
Message msg = new Message();
if(result)
msg.what = 0x00;
else
msg.what = 0x01;
handler.sendMessage(msg);
}
完成。
Android 线程更新UI报错 : 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()的解决办法
形同如下代码,在Thread中调用Toast显示错误信息: new Thread(new Runnable(){ @Override public void run() { try{ weatherD ...
- 在子线程中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 ...
- 工具类ToastUtil 避免在子线程中使用抛异常 "Can't create handler inside thread that has not called Looper.prepare()"
package com.example.kbr.utils; import android.view.Gravity; import android.widget.Toast; import io.r ...
- 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报 ...
- 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消息传递机制 ...
随机推荐
- 一个按比特位拷贝数据的函数copybits
一个按比特位拷贝数据的函数 没有进行特别的优化.其实还可以在拷贝源开始位置和目标开始位置是2的整数倍位置的时候进行优化. 说明 这个函数用于从src数组首地址跳过sbb个字节,又跳过ssb个比特位,拷 ...
- 2 DelayInterval延时间隔类——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 这里是时间相关类的第二个部分. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnb ...
- xdebug调试php程序
相关设置 xdebug.default_enable=1 默认是1,当错误出现时,堆栈跟踪会激活.可以在代码中通过xdebug_disable()来关闭它. xdebug.force_display_ ...
- django操作数据库之查询F,Q操作 和 seach搜索功能
# F 使用查询条件的值 # # from django.db.models import F # models.Tb1.objects.update(num=F('num')+1) # Q 构建搜索 ...
- Aufs与Devicemapper的关系
Aufs与Devicemapper的应用 Aufs是Docker最初采用的文件系统,由于Aufs未能加入到Linux内核,考虑到兼容性问题,加入了Devicemapper的支持.目前,除少数版本如Ub ...
- Angularjs与bootstrap.datetimepicker结合实现日期选择器
http://www.lovelucy.info/angularjs-best-practices.html http://damoqiongqiu.iteye.com/blog/1917971 ht ...
- 关闭EXCEL进程
//导入Windows类库,可以获得进程ID [DllImport("User32.dll", CharSet = CharSet.Auto)] pub ...
- windows下打开VMware虚拟机时提示内存不足的处理方法
参考:http://thinkpig007.blog.51cto.com/971471/1589831 以管理员身份运行vmware.exe即可 错误的错误提示: Not enough physica ...
- Pywinauto在Windows Twain Driver自动化测试中的应用研究
摘 要: 以Python为基础,结合对Twain Driver测试工具的具体需求,将Pywinauto引入到Twain Driver的自动化测试中.介绍了Pywinauto的基本概念,通过测试用例说 ...
- 如何让您的php也支持pthreads多线程
我们常常会碰到这样一种情况,开发环境在windows下开发,而生产环境确是linux.windows下能正常运行,上传到linux后却无法好好地玩耍了.然后开始了一轮尼玛式的疯狂的查找原因,最后发现是 ...