dialog子线程操作UI异常:Only the original thread that created a view hierarchy can touch its views
来源:互联网 发布:狙击手设置准星软件 编辑:程序博客网 时间:2024/06/06 14:22
前言
- 先说一下常规的几种解决方式,在给大家说最近一个遇到的常规方法不能解决的奇葩案例
解决方案
- 使用handler,通过handler去sendmessage()修改UI这里就不贴代码了,很简单.
- 利用activity的runOnUiThread方法.
runOnUiThread(new Runnable() { @Override public void run() { text.setText("修改UI"); } });
- 使用AsyncTask去修改UI,这个基本都知道,也不贴代码了.
- 在子线程里面,使用Looper.prepare()和Looper.loop(),
new Thread(new Runnable() { @Override public void run() { Looper.prepare(); text.setText("修改UI"); Looper.loop(); } });
- 子线程handler去getMainLooper(),然后修改UI
Handler handler = new Handler(getMainLooper()); handler.post(new Runnable() { @Override public void run() { text.setText("修改UI"); } });
奇葩案例
- 请谨慎使用全局变量,之前就是一个全局变量dialog,在子线程里面无论使用任何改变UI的方法,让其dismiss(),都会报异常.
- 记住一点,在哪条线程初始化的实例,就去哪条线程进行UI操作.
0 0
- dialog子线程操作UI异常:Only the original thread that created a view hierarchy can touch its views
- dialog子线程操作UI异常:Only the original thread that created a view hierarchy can touch its views
- android子线程控制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.
- 异常: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 解决Only the original thread that created a view hierarchy can touch its views异常
- android中用线程更新UI,报错only the original thread that created a view hierarchy can touch its views
- Work 工作子线程更新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的相关
- 报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的解决方案
- WeUI 入门教程
- C++笔试题 String类的实现 三大复制控制函数
- 非静态内部类为什么可以访问外部类的静态成员
- 51nod 1444 破坏道路
- Leetcode 299:Bulls and Cows
- dialog子线程操作UI异常:Only the original thread that created a view hierarchy can touch its views
- Integer和int的区别
- dialog子线程操作UI异常:Only the original thread that created a view hierarchy can touch its views
- Codeforces544C:Writing Code
- 设计模式:Builder模式(多个构造器参数时可显著改善可读性)
- HDU2001计算两点间的距离(C,Java用DecimalFormat格式控制)
- (转)Linux的chattr与lsattr命令详解
- arm平台函数传递参数,反汇编实例分析
- 地道的表达(二)