MainActivity中有一个按钮,绑定了save方法

  1. public void save(View view) {
  2. String title = titleText.getText().toString();
  3. String timelength = lengthText.getText().toString();
  4.  
  5. ExecutorService exec = Executors.newCachedThreadPool();
  6. exec.execute(new NewsService(getApplicationContext(),title,timelength));
  7. }

NewsService代码:

  1. @Override
  2. public void run() {
  3. String path = "http://192.168.0.102:8080/videonews/ManageServlet";
  4. Map<String,String> params = new HashMap<String,String>();
  5. params.put("title",title);
  6. params.put("timelength",timelength);
  7. boolean result=false;
  8. try {
  9. result = sendGETRequest(path,params);
  10. } catch (Exception e) {
  11. e.printStackTrace();
  12. }
  13.  
  14. if(result) {
  15. Toast.makeText(context, R.string.success, Toast.LENGTH_LONG).show();
  16. } else {
  17. Toast.makeText(context,R.string.error,Toast.LENGTH_LONG).show();
  18. }
  19. }

报错:

  1. 04-18 13:06:36.191 2284-2305/test.example.com.newsmanage E/AndroidRuntime FATAL EXCEPTION: pool-1-thread-1
  2. Process: test.example.com.newsmanage, PID: 2284
  3. java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  4. at android.os.Handler.<init>(Handler.java:200)
  5. at android.os.Handler.<init>(Handler.java:114)
  6. at android.widget.Toast$TN.<init>(Toast.java:336)
  7. at android.widget.Toast.<init>(Toast.java:100)
  8. at android.widget.Toast.makeText(Toast.java:250)
  9. at android.widget.Toast.makeText(Toast.java:277)
  10. at test.example.com.service.NewsService.run(NewsService.java:86)
  11. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  12. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  13. at java.lang.Thread.run(Thread.java:818)

查了资料,说是Android中不能在子线程中来刷新UI。如果要实现你这功能的话。建议是在你的子线程中添加hander来发送消息更新线程。

下面这样做就OK了

1.在Activity中增加如下代码

  1. private Handler myHandler = new Handler() {
  2. @Override
  3. public void handleMessage(Message msg) {
  4. switch (msg.what) {
  5. case 0x00 :
  6. Toast.makeText(getApplicationContext(),"save success",Toast.LENGTH_SHORT).show();
  7. break;
  8. default:
  9. Toast.makeText(getApplicationContext(),"save fail",Toast.LENGTH_SHORT).show();
  10. }
  11.  
  12. }
  13. };

2.启动线程时,将handler传入:

  1. exec.execute(new NewsService(getApplicationContext(),myHandler,title,timelength));

3.在线程中,发送消息给handler:

  1. @Override
  2. public void run() {
  3. String path = "http://192.168.0.102:8080/videonews/ManageServlet";
  4. Map<String,String> params = new HashMap<String,String>();
  5. params.put("title",title);
  6. params.put("timelength",timelength);
  7. boolean result=false;
  8. try {
  9. result = sendGETRequest(path,params);
  10. } catch (Exception e) {
  11. e.printStackTrace();
  12. }
  13. Message msg = new Message();
  14. if(result)
  15. msg.what = 0x00;
  16. else
  17. msg.what = 0x01;
  18. handler.sendMessage(msg);
  19. }

完成。

Android 线程更新UI报错 : Can't create handler inside thread that has not called Looper.prepare()的更多相关文章

  1. 关于子线程使用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 ...

  2. 在子线程中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 ...

  3. 【转】在子线程中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 ...

  4. 转 在子线程中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 ...

  5. 工具类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 ...

  6. Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()

    问题: 写了一个sdk给其他人用,提供一个回调函数,函数使用了handler处理消息 // handler监听网络请求,完成后操作回调函数 final Handler trigerGfHandler ...

  7. 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报 ...

  8. OkHttp下载文件中途断网报Can't create handler inside thread that has not called Looper.prepare()异常的解决办法

    最近做项目时出现个问题. 在一个基类中,创建一个Handler对象用于主线程向子线程发送数据,代码如下: this.mThirdHandler = new Handler(){ @Override p ...

  9. 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消息传递机制 ...

随机推荐

  1. gedit配置记

    gedit配置记 起因 突然感觉sublime用用这里那里不方便(虽然很好看> >),然后稍微手调了一下gedit发现gedit还是非常可用的(雾)... 阶段一 我感觉sublime各种 ...

  2. bootstrap常用的定义风格

    primary()     首选项 success()     成功 info()        一般信息 warning()       警告 danger()         危险 <bod ...

  3. 前端开发必须知道的JS(二) 闭包及应用

    http://www.cnblogs.com/ljchow/archive/2010/07/06/1768749.html 在前端开发必须知道的JS(一) 原型和继承一文中说过下面写篇闭包,加之最近越 ...

  4. ERROR Cannot determine the location of the VS Common Tools Folder

    参考:ERROR Cannot determine the location of the VS Common Tools Folder   http://blog.csdn.net/m3728975 ...

  5. 页面加载时执行JQ代码

    $(function () { //jq加载时执行的这里面是 $("#ss").append("<strong>这是新加的</strong>&qu ...

  6. Selenium WebDriver 处理table

    首先,html table是由 table 元素以及一个或多个 tr.th 或 td 元素组成. for example: 这是一个简单的html table: 源码如下: <html> ...

  7. ACM/ICPC 之 一道不太简单的DP面试题(Geeksforgeeks)

    题面来源:geeksforgeeks/1993 题解:geeksforgeeks 题目简述:给一个m*n的矩阵,计算从(1,1)到(m,n)的所有不回退路径中,经过k次转向后的路径有多少条 输入T个样 ...

  8. ios 中清除webView的缓存

    在UIWebView下,可以使用 [[NSURLCache sharedURLCache] removeAllCachedResponses];//清除缓存 来实现清除缓存,但当替换使用WKWebVi ...

  9. 查看Linux内核

    方法一: 命令: uname -a 作用: 查看系统内核版本号及系统名称 方法二: 命令: cat /proc/version 作用: 查看目录"/proc"下version的信息 ...

  10. IOS - 真机测试

    转:版权声明:本文由本人亲自一步步完成,并细心排版生成,望广大同仁尊重一下本人的劳动成果,转载请注明出处,原文地址http://my.oschina.net/joanfen/blog/167730 一 ...