Android异常:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
来源:互联网 发布:网络购物平台 英文 编辑:程序博客网 时间:2024/06/08 06:11
Android异常:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
出现这个异常的原因是你在子线程中修改了主线程中的组件的状态或数据。.翻译过来就是:只有创建这个控件的线程才能去更新该控件的内容。
android规定:只有在原始线程中(主线程,UI线程)才能修改显示组件。
解决方案:1、使用Handler机制。2、使用AsyncTask
方案1概述:handler机制
如下就是handler的简单工作原理图:
既然android给我们提供了Handler机制来解决这样的问题,请看如下代码:
class MyOnclickListener implements OnClickListener {@Overridepublic void onClick(View v) {switch (v.getId()) {// 响应Handler异步方式case R.id.downbtn1:// 显示进度对话框,这里也可以使用进度条,在handleMessage方法中更新进度dialog = ProgressDialog.show(DownLoadImageActivity.this, "","正在下载,请稍等···");// 新建一个子线程来发送消息new Thread() {@Overridepublic void run() {try {// 让ProgressDialog显示一会儿。。。。Thread.sleep(2000);URL url = new URL(PATH);// 建立网络连接HttpURLConnection conn = (HttpURLConnection) url.openConnection();InputStream inputStream = conn.getInputStream();// 获取图片数据bitmap = BitmapFactory.decodeStream(inputStream);inputStream.close();Message message = new Message();message.what = 1;// 发送消息到消息队列中handler.sendMessage(message);} catch (Exception e) {Message message = new Message();message.what = -1;handler.sendMessage(message);e.printStackTrace();}}}.start();break;// 响应AsyncTask异步方式case R.id.downbtn2:new DownImageTask().execute(PATH);break;}}}// Handler异步方式下载图片private Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {ImageView imageView;switch (msg.what) {case 1:// 下载成功imageView = (ImageView) findViewById(R.id.image);dialog.dismiss();imageView.setImageBitmap(bitmap);break;case -1:// 下载失败使用默认图片imageView = (ImageView) findViewById(R.id.image);dialog.dismiss();imageView.setBackgroundResource(R.drawable.icon);break;}};};
我们就通过Handler机制来处理了子线程去更新UI线程控件问题,Andrid开发中要经常用到这种机制。
方式2以后再做介绍。 1 0
- Android异常:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- 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
- 16-07-24 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a v
- 异常:android.view.ViewRootImpl$CalledFromWrongThreadException
- E/错误(3907): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the o
- .AppException: android.view.ViewRootImpl$CalledFromWrongThreadException: Only t
- android异常:android.view.ViewRootImpl$CalledFromWrongThreadException
- android.view.ViewRootImpl$CalledFromWrongThreadException:
- 第十三周纯虚析构函数
- java开发webservice的几种方式
- salt一键部署jdk
- android中xml tools属性详解
- 第15周 程序阅读六
- Android异常:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- linux shell 文件操作总结
- 欢迎使用CSDN-markdown编辑器
- circle dependency
- tuzicms本地环境搭建 系统安装图文教程
- javascripr 中什么是事件
- win32 进程查找相关代码
- 洗牌
- dig查询dns解析