android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
来源:互联网 发布:舟山淘宝代运营诈骗案 编辑:程序博客网 时间:2024/06/05 17:14
今天继续在编写app,后来女朋友来消息,按了home将app后台运行,再打开时崩了,
,报了如下错误:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
(真特么低级的错误,去年犯的错今年还犯)
private void setdate() { new Thread(new Runnable() { @Override public void run() { String strURL = "http://jirenguapi.applinzi.com/weather.php"; URL url = null; try { url = new URL(strURL); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); InputStreamReader input = new InputStreamReader(httpConn .getInputStream(), "utf-8"); BufferedReader bufReader = new BufferedReader(input); String line = ""; StringBuilder contentBuf = new StringBuilder(); while ((line = bufReader.readLine()) != null) { contentBuf.append(line); } date_weather.setText(contentBuf); } catch (MalformedURLException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start();}改成如下代码,使用了handler异步更新,测试后不再崩溃,大致原理是在主线程创了一个handler,接收来自子线程的数据并更新ui
private Handler handler_setdate = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); String message = msg.obj.toString(); date_weather.setText(message); }};private void setdate() { new Thread(new Runnable() { @Override public void run() { String strURL = "http://jirenguapi.applinzi.com/weather.php"; URL url = null; try { url = new URL(strURL); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); InputStreamReader input = new InputStreamReader(httpConn .getInputStream(), "utf-8"); BufferedReader bufReader = new BufferedReader(input); String line = ""; StringBuilder contentBuf = new StringBuilder(); while ((line = bufReader.readLine()) != null) { contentBuf.append(line); } Message msg = new Message(); msg.obj= contentBuf; handler_setdate.sendMessage(msg); } catch (MalformedURLException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start();}
阅读全文
0 0
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a v
- ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy
- CalledFromWrongThreadException: Only the original thread that created a view
- CalledFromWrongThreadException: Only the original thread that created a view
- android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view
- android.view.viewroot$calledfromwrongthreadexception: only the original thread that created a view h
- 16-07-24 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that
- CalledFromWrongThreadException:Only the original thread that created a view hierarchy can touch its
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- Java基础之运算符
- 数值分析常用的几个小程序C++实现
- 51nod 1201 整数划分(dp)
- cordova开发教程
- NandFlash ECC 校验算法原理与实现
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- Android打开系统相册页面
- 一些小知识点
- 二叉树的递归遍历以及非递归遍历
- ubuntu 14.04 开启root登录
- 面试(六) 多线程
- c语言获取用户输入字符串是scanf和gets的区别
- [人工智能]深度学习卷积神经网络的秒懂各种操作
- 120. Triangle