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消息传递机制 ...
随机推荐
- 使用uwsgi 部署python web服务
uwsgi, wsgi协议的一个很好的实现,源码在这里:https://github.com/unbit/uwsgi c语言编写,有兴趣可以研究下. 上DEMO: wsgi_server.py def ...
- dtw算法优化(重写C语言版本)
1.缩小搜索范围 2.降低内存消耗
- 【System】shell 实现 bat 的pause功能
read -rsp $'Press enter to continue...\n' 参考资料: http://stackoverflow.com/questions/92802/what-is-the ...
- Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Java中 final static super this instanceof 关键字用法
一.final关键字 final可以修饰变量.方法及类: 1.当定义一个final变量时,jvm会将其分配到常量池中,其所修饰的对象只能赋值一次,对基本类型来说是其值不可变,引用类型(包括作为函数形参 ...
- [转]C++中四种类型转换符的总结
C++中四种类型转换符的总结 一.reinterpret_cast用法:reinpreter_cast<type-id> (expression) reinterpret_cast操 ...
- sruts2 自定义类型转换器
1.1.1 Struts2中自定义类型转换器:(了解) 类型转换的过程是双向的过程: JSP---->Action参数提交:String---Date. Action---->JSP ...
- centos7 systemctl 启动 Redis 失败
转自:http://sloger.info/posts/systemd-failed-to-start-redis-in-gentoo 今天启动 Redis 时阻塞很长时间,之后显示启动失败,启动状态 ...
- Python自动化运维工具fabric的安装
使用shell命令进行复杂的运维时,代码往往变得复杂难懂,而使用python脚本语言来编写运维程序,就相当于开发普通的应用一样,所以维护和扩展都比较简单,更重要的是python运维工具fabric能自 ...
- expression<Func<object,Bool>> 及 Func<oject,bool>用法
using System;using System.Collections.Generic;using System.Linq;using System.Linq.Expressions;using ...