android报错:Only the original thread that created a view hierarchy can touch its views.
问题的报错位置是我用handler写了一个定时消息,在handleMessage()方法里直接写了ui更新操作,
这个报错的原因一般是因为在子线程中直接操作UI导致的(eg, setText())。因为Android中相关的view和控件操作都不是线程安全的,所以Android禁止在非UI线程更新UI。
ps. 这里所指的操作一般是能会导致控件重绘(invalidate)的操作,在子线程执行setTextColor此类UI操作不会报错
**那么,如果子线程有需要操作UI的需求怎么办呢?一般来说,可以通过如下几种方式来实现。其原理都是通过间接调用,在主线程中去操作UI:
- 使用View.post方法,post一个Runnable对象,在run方法中操作UI**
final String text = Thread.currentThread().getId() + "_" + i++;
final TextView txtTime1 = (TextView)mainActivity.findViewById(R.id.txtTime1);
txtTime1.post(new Runnable() {@Overridepublic void run() {txtTime1.setText("[View.post]" + text);}
});
- 使用Activity.runOnUiThread方法,传入一个Runnable对象,在run方法中操作UI
final TextView txtTime2 = (TextView)mainActivity.findViewById(R.id.txtTime2);
mainActivity.runOnUiThread(new Runnable() {@Overridepublic void run() {txtTime2.setText("[Activity.runOnUiThread]");}
});
- 在Activity中创建一个Handler对象,在子线程中通过handle.post方法,传入一个Runnable对象,在run方法中操作UI
final TextView txtTime4 = (TextView)mainActivity.findViewById(R.id.txtTime4);
mainActivity.mHandler.post(new Runnable() {@Overridepublic void run() {txtTime4.setText("[Handler.post]" + text);}
});
- 在Activity中创建一个Handler对象,并且在handlerMessage方法中针对指定的消息去操作UI,而消息则是有子线程通过handler.sendMessage方法发送
1)在Activity的onCreate方法中创建一个Handler对象,并在收到自定义消息UPDATE_TIME_ID之后操作UI
mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);if (msg.what == UPDATE_TIME_ID) {String strTime = msg.getData().getString(UPDATE_TIME_KEY);((TextView)findViewById(R.id.txtTime3)).setText(strTime);} }
};
2) 在子线程中通过handler.sendMessage方法发送ID为UPDATE_TIME_ID的消息
Message updateTimeMessage = Message.obtain();
Bundle bundle = new Bundle();
bundle.putString(UPDATE_TIME_KEY, "[Handle.sendMessage]" + text);
updateTimeMessage.what = MainActivity.UPDATE_TIME_ID;
updateTimeMessage.setData(bundle);
mainActivity.mHandler.sendMessage(updateTimeMessage);
android报错:Only the original thread that created a view hierarchy can touch its views.相关推荐
- android开发 bug问题解决:Only the original thread that created a view hierarchy can touch its views
android开发 bug问题解决:Only the original thread that created a view hierarchy can touch its views. 翻译:只有创 ...
- Only the original thread that created a view hierarchy can touch its views异常
写代码的时候碰到android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that crea ...
- Only the original thread that created a view hierarchy can touch its views——Handler的使用
今天写了一个更新UI的小例子,没想到出了log打印了这样一个错误:Only the original thread that created a view hierarchy can touch it ...
- only the original thread that created a view hierarchy can touch its views解决方案
Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that c ...
- Only the original thread that created a view hierarchy can touch its views
今天发现一个界面setText 的时候出现了这个问题,看到报错,大致就是界面还没创建呢,就使用seText 了 这个是自己的理解, 处理方法就是 添加一个延时操作, runOnUiThread(new ...
- Only the original thread that created a view hierarchy can touch its views.
这个问题是由于再非主线程中更新了Android的UI,找到可能是在非主线程更新UI的方法,使用使其再handler中更新ui,问题基本上就能够解决. 转载于:https://www.cnblogs.c ...
- 出现Only the original thread that created a view hierarchy can touch its views.的错误
先看这句话的翻译哈.只有创建视图层次结构的原始线程才能触摸其视图. 一般这种错误出现在子线程中操作主线程的控件的操作中,比如更新等操作. 解决这类问题,需要使用Handler.关于Handler是什么 ...
- Android报错——connect failed: EHOSTUNREACH (No route to host)
2019独角兽企业重金招聘Python工程师标准>>> Android报错--connect failed: EHOSTUNREACH (No route to host) 没有路由 ...
- hive启动报错:Exception in thread “main“ java.lang.NoSuchMethodError: com.google.common.base.Precondition
报错如下 Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Precon ...
最新文章
- Java EE---使用Spring框架创建Market小项目
- 对中国医学影像AI产品商业化的思考
- mysql 乘法拼接字符串_【原创】利用MySQL 的GROUP_CONCAT函数实现聚合乘法
- Linux设备驱动开发概述
- Java两个列表根据属性去重_java8 如何对list操作 根据某一个字段进行判断去重对另一个字段进行累加 最终返回list?...
- bitbucket初次使用
- mysql监控sql排名_监控数据库性能的SQL汇总
- java 采样_Java编程实现beta分布的采样或抽样实例代码
- TEN网格数据导入oracle,开源-Solidity 分散的oracle网络的示例链链接。-糯米PHP
- 安卓案例:帧式布局演示(切换颜色)
- angualr8观察者模式_观察者模式
- SpringBoot整合kafka案例
- python操作数据库的几种方法_python对mysql数据库操作的三种不同方式
- 论文阅读笔记(五)——FD-MOBILENET
- 元组-不仅仅是不可变的列表
- arcmap中图斑面积代表_arcmap计算面积_ArcMap怎么重计算图斑面积?arcmap使用手册_arcmap计算面积...
- disabled spring cloud bus interation with spring cloud sleuth zipkin
- 6个常见的API接口在线管理平台
- Linux图形子系统
- 音视频基础认知——ISP与DSP