Android: Only the original thread that created a view hierarchy can touch its views 异常
最近自己再写一个小项目练手,创建一个线程从网络获取数据然后显示在 recyclerView 上。写好后发现页面能够显示,但是有时候会把请求的数据显示过来,有时候不会。点开 android monitor 一看,有一个提示 :
Only the original thread that created a view hierarchy can touch its views.
异常的意思是说只有创建这个view的线程才能操作这个 view,普通会认为是将view创建在非UI线程中才会出现这个错误。
本来我想将就下,能看到算了的,说明我会简单使用 fragment 了。不过作为程序员我们肯定要寻根问底的啊。
这段请求数据代码如下所示:
new Thread(new Runnable() {
@Override
public void run() {
try {
String url = "";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).method("GET", null).build();
okhttp3.Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String responseString = (response.body() == null ? "" : response.body().string());
......解析数据
WidgetActionEvent event = new WidgetActionEvent(WidgetActionEvent.ACTION_CLICK);
event.object = feedModel;
EventBus.getDefault().post(event);
//iLoadData.loadData(feedModel);
} else {
Log.i(TAG, "okHttp is request error");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
数据请求之后,解析,并采用 eventbus 来传送数据。
ps :如果你是在 mainActivity 中调用上述代码,是不会产生的异常的,因为都是运行在主线程中。
变形一 : 崩溃
于是我换了一种形式来传递数据,这次采用回调的方式,也就是上面被注释掉的那行代码:
iLoadData.loadData(feedModel);
这次不用 eventbus 竟然崩溃了......我能怎么办,我也很无奈啊。
FATAL EXCEPTION: Thread-932
Process: example.hope.mvpwithrecyclerview, PID: 4916
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
解决办法:采用 handle
实现代码如下:
/**
* 发起网络请求
*/
public static void okHttp_synchronousGet(final Handler handler) {
new Thread(new Runnable() {
@Override
public void run() {
try {
String url = "url";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).method("GET", null).build();
okhttp3.Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String responseString = (response.body() == null ? "" : response.body().string());
......解析数据
handler.sendMessage(handler.obtainMessage(22, feedModel));
} else {
Log.i(TAG, "okHttp is request error");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
然后再 fragment 添加下面代码用来处理传过来的数据:
/**
* 接收解析后传过来的数据
*/
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Object model = (Object) msg.obj;
showPictures(model);
}
};
这样就能后完美的解决这个问题了。
Android: Only the original thread that created a view hierarchy can touch its views 异常的更多相关文章
- 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.
在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...
- 开发错误记录1:解决:Only the original thread that created a view hierarchy can touch its views.
今天在项目中要使用圆角头像,导入开源 CircleImageView ,然后setImageBitmap()时 运行时就会发现,它会报一个致命性的异常:: · ERROR/AndroidRuntime ...
- Only the original thread that created a view hierarchy can touch its views
在调试软件的时候出现如下的错误: 01-05 20:53:36.492: E/ZZShip(2043): android.view.ViewRootImpl$CalledFromWrongThread ...
- Only the original thread that created a view hierarchy can touch its views解决办法
这周操作系统作业布置了一个作业,内容是做个小软件,来模拟消费者生产者问题,作业实现起来不来,因为之前写过这个算法,所以关键步骤就是在消费和生产的时候更新缓存区的UI控件就行,之后问题就来了,出现了标题 ...
- 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用
在跟随教程学习到显示web页面的html源码时报错:Only the original thread that created a view hierarchy can touch its views ...
- 子线程调用invalidate()产生“Only the original thread that created a view hierarchy can touch its views.”原因分析
目录 1.异常出处 2.从View.invalidate()方法开始分析 3.ViewRootImpl如何与View进行关联:从Activity的setContentView开始分析 3.1 最顶层的 ...
- andriod 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用
package com.example.yanlei.myapplication; import android.media.MediaMetadataRetriever; import androi ...
- 解决Only the original thread that created a view hierarchy can touch its views
这种异常出现在子线程中处理UI操作产生的异常,将UI操作放在主线程中就OK了
- "Only the original thread that created a view hierarchy can touch its views.” 解决方法
这个主要总是,开启的线程和 UI 线程(主线程)不是同一个线程.可以Runnable方式避免,如下例所示就可以解决这个问题了. public static void updateText(Activi ...
随机推荐
- JVM菜鸟进阶高手之路十(基础知识开场白)
转载请注明原创出处,谢谢! 最近没有什么实战,准备把JVM知识梳理一遍,先以开发人员的交流来谈谈jvm这块的知识以及重要性,依稀记得2.3年前用solr的时候老是经常oom,提到oom大家应该都不陌生 ...
- 使用jquery的方法和技巧2,点击多选框的jquery响应
使用jquery来控制多选框的变化 功能描述: 1.第一层 当选中后台应用(App1)时,所有多选框都被选择. 当取消选中后台应用(App1)时,所有多选框都被取消选择. 第一层的逻辑如下: 2.第二 ...
- TComboBox组件重要属性和事件
TComboBox组件的重要属性 CharCase--------此属性用于设置编辑框内文字的大小写 DropDownCount---此属性用于设置当用户下拉组合框时不需要加滚动条就能显示的项的个数 ...
- C#设计模式之五创建者模式(Builder)【创建型】
一.引言 今天我们要讲讲Builder模式,也就是建造者模式,当然也有叫生成器模式的.在现实生活中,我们经常会遇到一些构成比较复杂的物品,比如:电脑,它就是一个复杂的物品,它主要是由CPU.主板.硬 ...
- PHP和JS判断变量是否定义
PHP中: 通过isset(变量名)来判断,定义返回true/未定义返回false JS中: 通过typeof来判断.
- Theano学习-scan循环
\(1.Scan\) 通用的一般形式,可用于循环 减少和映射(对维数循环)是特殊的 \(scan\) 对输入序列进行 \(scan\) 操作,每一步都能得到一个输出 \(scan\) 能看到定义函数的 ...
- php中常用的字符串长度函数strlen()与mb_strlen()实例解释
int strlen ( string $string ) int strlen ( string $string ) 获取给定字符串的[字节]长度 成功则返回字符串$string的长度,如果$s ...
- SQL监测语句
SELECT top 20 qs.creation_time,last_execution_time,total_physical_reads,total_logical_reads,total_lo ...
- 【转】python time模块详解
python 的内嵌time模板翻译及说明 一.简介 time模块提供各种操作时间的函数 说明:一般有两种表示时间的方式: 第一种是时间戳的方式(相对于1970.1.1 00:00:0 ...
- Elasticsearch分片、副本与路由(shard replica routing)
本文讲述,如何理解Elasticsearch的分片.副本和路由策略. 1.预备知识 1)分片(shard) Elasticsearch集群允许系统存储的数据量超过单机容量,实现这一目标引入分片策略sh ...