android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
2017-05-15 18:48
531 查看
今天继续在编写app,后来女朋友来消息,按了home将app后台运行,再打开时崩了,
,报了如下错误:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
大致意思就是,我在非主线程更新ui了,所以崩了
(真特么低级的错误,去年犯的错今年还犯)
改成如下代码,使用了handler异步更新,测试后不再崩溃,大致原理是在主线程创了一个handler,接收来自子线程的数据并更新ui
,报了如下错误:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
大致意思就是,我在非主线程更新ui了,所以崩了
(真特么低级的错误,去年犯的错今年还犯)
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(); }
相关文章推荐
- 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 view hierarchy can touch its views.
- android ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a v
- android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view
- 16-07-24 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that
- android.view.viewroot$calledfromwrongthreadexception: only the original thread that created a view h
- ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- Android异常:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- 子线程中处理UI,抛异常 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- Android异常:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- CalledFromWrongThreadException: Only the original thread that created a view
- E/错误(3907): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the o