问题的报错位置是我用handler写了一个定时消息,在handleMessage()方法里直接写了ui更新操作,
这个报错的原因一般是因为在子线程中直接操作UI导致的(eg, setText())。因为Android中相关的view和控件操作都不是线程安全的,所以Android禁止在非UI线程更新UI。
ps. 这里所指的操作一般是能会导致控件重绘(invalidate)的操作,在子线程执行setTextColor此类UI操作不会报错

**那么,如果子线程有需要操作UI的需求怎么办呢?一般来说,可以通过如下几种方式来实现。其原理都是通过间接调用,在主线程中去操作UI:

  1. 使用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);}
});
  1. 使用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]");}
});
  1. 在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);}
});
  1. 在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.相关推荐

  1. 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. 翻译:只有创 ...

  2. Only the original thread that created a view hierarchy can touch its views异常

    写代码的时候碰到android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that crea ...

  3. 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 ...

  4. 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 ...

  5. Only the original thread that created a view hierarchy can touch its views

    今天发现一个界面setText 的时候出现了这个问题,看到报错,大致就是界面还没创建呢,就使用seText 了 这个是自己的理解, 处理方法就是 添加一个延时操作, runOnUiThread(new ...

  6. Only the original thread that created a view hierarchy can touch its views.

    这个问题是由于再非主线程中更新了Android的UI,找到可能是在非主线程更新UI的方法,使用使其再handler中更新ui,问题基本上就能够解决. 转载于:https://www.cnblogs.c ...

  7. 出现Only the original thread that created a view hierarchy can touch its views.的错误

    先看这句话的翻译哈.只有创建视图层次结构的原始线程才能触摸其视图. 一般这种错误出现在子线程中操作主线程的控件的操作中,比如更新等操作. 解决这类问题,需要使用Handler.关于Handler是什么 ...

  8. Android报错——connect failed: EHOSTUNREACH (No route to host)

    2019独角兽企业重金招聘Python工程师标准>>> Android报错--connect failed: EHOSTUNREACH (No route to host) 没有路由 ...

  9. 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 ...

最新文章

  1. Java EE---使用Spring框架创建Market小项目
  2. 对中国医学影像AI产品商业化的思考
  3. mysql 乘法拼接字符串_【原创】利用MySQL 的GROUP_CONCAT函数实现聚合乘法
  4. Linux设备驱动开发概述
  5. Java两个列表根据属性去重_java8 如何对list操作 根据某一个字段进行判断去重对另一个字段进行累加 最终返回list?...
  6. bitbucket初次使用
  7. mysql监控sql排名_监控数据库性能的SQL汇总
  8. java 采样_Java编程实现beta分布的采样或抽样实例代码
  9. TEN网格数据导入oracle,开源-Solidity 分散的oracle网络的示例链链接。-糯米PHP
  10. 安卓案例:帧式布局演示(切换颜色)
  11. angualr8观察者模式_观察者模式
  12. SpringBoot整合kafka案例
  13. python操作数据库的几种方法_python对mysql数据库操作的三种不同方式
  14. 论文阅读笔记(五)——FD-MOBILENET
  15. 元组-不仅仅是不可变的列表
  16. arcmap中图斑面积代表_arcmap计算面积_ArcMap怎么重计算图斑面积?arcmap使用手册_arcmap计算面积...
  17. disabled spring cloud bus interation with spring cloud sleuth zipkin
  18. 6个常见的API接口在线管理平台
  19. Linux图形子系统
  20. 音视频基础认知——ISP与DSP

热门文章

  1. React的非受控组件和受控组件
  2. 干货 | PCB设计中焊盘的种类和设计标准
  3. 一篇关于营销的功能需求分析
  4. 【3小时学会C语言】横向对比/纵向剖析,轻松学习C语言
  5. 计算机属性没有共享,win10系统本地连接属性里没有共享选项的具体方案
  6. Latex 跨页图片跑到最后一页问题
  7. C语言 - 计算n的阶乘(n!)
  8. Adobe系列软件彻底清理方法(手动删除)
  9. Mac用户学Python-Day1:安装与环境配置
  10. 使用uniapp编写词霸每日一句页面