【BUG】Only the original thread that created a view hierarchy can touch its views
来源:互联网 发布:立方体全景图生成算法 编辑:程序博客网 时间:2024/06/05 07:48
出现错误的根源在于:
调用到 ViewRootImpl
的 invalidateChildInParent(int[] location, Rect dirty)
方法的时候会去检测线程,也就是checkThread()
。checkThread()
里面会判断当前线程是不是主线程,如果不是的就抛出异常。
出现该问题的错误场景有如下几个:
一.
textView = (TextView) findViewById(R.id.tv);new Thread(new Runnable() { public void run() { SystemClock.sleep(3000); textView.invalidate(); }}).start();
通过上述情况不难看出,导致异常的原因并不在给 TextView
设置文本或者给ImageView
设置图片,真正的原因是调用这些方法之后都会导致View的重绘,然后重绘的时候系统回去检测当前线程,如果是非主线程,则抛出异常。
一般引起invalidate()操作的函数如下:
1、直接调用invalidate()方法,请求重新draw(),但只会绘制调用者本身。
2、setSelection()方法 :请求重新draw(),但只会绘制调用者本身。
3、setVisibility()方法 : 当View可视状态在INVISIBLE转换VISIBLE时,会间接调用invalidate()方法,继而绘制该View。
4 、setEnabled()方法 : 请求重新draw(),但不会重新绘制任何视图包括该调用者本身。
二.
textView = (TextView) findViewById(R.id.tv); new Thread(new Runnable() { public void run() { SystemClock.sleep(3000); textView.setText("我是文本来自子线程"); } }).start();
0 0
- 【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的相关
- 报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错误
- 报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错误解决
- 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的相关
- 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的解决方案
- 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
- 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的解决方案
- 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
- 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.
- LeetCode OJ 56. Merge Intervals
- Intellij IDEA 快捷键整理
- 去掉list中重复元素并合并相同对象
- 低值易耗品管理系统
- Gym
- 【BUG】Only the original thread that created a view hierarchy can touch its views
- cordova安卓平台的搭建
- 求质数(素数)算法及其改进
- Codeforces Round #403 (Div. 2) C. Andryusha and Colored Balloons
- “ValidateAddInXMLTask” 任务意外失败
- iOS验证身份证号的有效性(仅18位号)
- ios opengl hd picture show
- sky3888 Top Up Unbelievable Magic Show Simsalabim Slot
- 缓存之EHCache(二)