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
这句话的意思是,只有创建这个view的原始线程才能接触到它,意思就是我UI控件的更新不在原始线程里面,我源码这样的
//Producter.java
switch (producter.charAt(producter.length()-1)){
case '0':
view0.setImageResource(R.drawable.goods);
Log.d("生产者:我做了",producter);
break;
其实到这里我大概知道了我为什么错了,可是又不想重写逻辑,换到主线程来改UI,于是查了一下资料,在安卓中,UI更新不是线程安全的,所以只能在主线程中更新UI,那么,到底有没有问题在子线程中更新UI呢,答案肯定是可以的,我找到了一个办法,那就是通过View类中的post函数来更新,首先我把做法的源码贴上来
switch (producter.charAt(producter.length()-1)){
case '0':
view0.post(new Runnable() {
@Override
public void run(){
view0.setImageResource(R.drawable.goods);
Log.d("生产者:我做了",producter);
}
});
break;
可以看到,这里启动了一个新的线程,但是,问题来了,为什么这样就可以了?UI更新不还是在非主线程中吗?
说实话我想了挺久没想太明白,看了一下view.post的源码也没整得太明白,可能因为我接触安卓不太久的原因,火候还不够,但是问题摆在这里还是得解决...我还是贴个大佬很详细的讲解吧
里面有很详细的代码讲解,一行一行跟进的
另外,还贴几个更新在子线程中更新UI的方法
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.” 解决方法
这个主要总是,开启的线程和 UI 线程(主线程)不是同一个线程.可以Runnable方式避免,如下例所示就可以解决这个问题了. public static void updateText(Activi ...
- Android: Only the original thread that created a view hierarchy can touch its views 异常
最近自己再写一个小项目练手,创建一个线程从网络获取数据然后显示在 recyclerView 上.写好后发现页面能够显示,但是有时候会把请求的数据显示过来,有时候不会.点开 android monito ...
- 开发错误记录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 ...
- 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.
在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...
- 错误: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了
随机推荐
- 812. Largest Triangle Area
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 2018.10.25 洛谷P4187 [USACO18JAN]Stamp Painting(计数dp)
传送门 其实本来想做组合数学的2333. 谁知道是道dpdpdp. 唉只能顺手做了 还是用真难则反的思想. 这题我们倒着考虑,只需要求出不合法方案数就行了. 这个显然是随便dpdpdp的. f[i]f ...
- 用react脚手架新建项目
1.全局安装 create-react-app脚手架 [可能需要管理员权限]npm install -g create-react-app 2.创建项目 create-react-app projec ...
- DocumentFragment类型
nodeType 11 nodeName #document-fragment nodeValue NULL parentNode null createdocumentfragment()方法创建了 ...
- REST WebService与SOAP WebService的比较
在SOA的基础技术实现方式中WebService占据了很重要的地位,通常我们提到WebService第一想法就是SOAP消息在各种传输协议上交互.近几年REST的思想伴随着SOA逐渐被大家接受,同时各 ...
- MIT Molecular Biology 笔记7 调控RNA
视频 https://www.bilibili.com/video/av7973580/ 教材 Molecular biology of the gene 7th edition J.D. Wat ...
- DOS下如何打开程序
cd c: #先切回主盘,因为最开始在C:\Users\Administrator这个目录下 cd “指定文件所在的盘:” #切换到文件所在盘 print "文件目录&quo ...
- centos网络配置(手动设置,自动获取)的2种方法3
不知道为什么最近一段时间网络特别的慢,还老是断,断的时候,局域网都连不上,当我手动设置一下ip后就可以了,搞得我很无语.下面是2种设置网络连接的方法,在说怎么设置前,一定要做好备份工作,特别是对于新手 ...
- MyBatis高级篇之整合ehcache缓存框架
MyBatis高级篇之整合ehcache缓存框架 2017-09-01 0 Comments 1,671 Views 0 Times 一.前言 MyBatis为我们提供了Cache接口,也提供 ...
- hadoop两大核心之一:MapReduce总结
MapReduce是一种分布式计算模型,由Google提出,主要用于搜索领域,MapReduce程序 本质上是并行运行的,因此可以解决海量数据的计算问题. MapReduce任务过程被分为两个处理阶段 ...