解决Only the original thread that created a view hierarchy can touch its views
来源:互联网 发布:淘宝主播合作 编辑:程序博客网 时间:2024/05/17 01:14
很多网友在Android中使用多线程处理UI相关内容时可能会发现Logcat提示Only the original thread that created a view hierarchy can touch its views这样的错误,这主要是Android的相关View和控件不是线程安全的,我们必须做独立的处理这点比J2ME麻烦一些,这里Android给 我们提供了很多方法,有关线程的,我们需要了解下J2ME中一些传统的线程创建方法,比如Runnable或直接new Thread(),大家需要了解UI线程、worker线程以及一些概念。今天android开发网说一种简单的方法除了异步任务AsyncTask外使 用Handler可以很好的处理,和Win32的消息很像。
首先我们需要明白,主线程或者这里说的原始线程original thread 一般情况下是UI线程,当然UI线程并不一定是主线程,我们不能长时间的阻塞该应用,在Android平台上可能会产生类似Force close或Wait这样的对话框这里我们成为ANR,这里除了使用ProgressDialog方式给用一个动态的进度代表当前处理并没有中断可能需要 一些时间,所以android123告诉大家相关的网络处理可以使用工作者线程,但是worker 线程不能处理显示元素即UI相关的View或 Widget包中的高层的控件,所以通过一个Handler对象可以很好的传递Runnable或Message ,下面我们用一个简单的例子来描述
final Handler cwjHandler = new Handler();
final Runnable mUpdateResults = new Runnable() {
public void run() {
updateUI();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NetworkOperation();//一个很费时间的I/O操作,比如网络或文件读写等等。
}
protected void NetworkOperation() {
Thread t = new Thread() {
public void run() {
doSomething(); //处理得到结果了,这里一些内容保存在主类的成员变量中
cwjHandler.post(mUpdateResults); //高速UI线程可以更新结果了
}
};
t.start();
}
private void updateUI() {
//详细的更新
}
有关Java以及Android的线程,我们将在以后的内容中介绍到,本次相关内容讲到这些,以后我们会告诉大家一些线程同步以及死锁问题的解决方法,总体而言感觉相对于Windows Mobile平台,Android上的线程消耗过大,而Symbian的活动对象又相对保守,总的而言Android线程相对简单。
找了些线程的问题与大家共勉
- 报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.
- Android 解决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的解决方案
- 椭圆曲线密码体制(ECC)
- Ubuntu常用命令大全(转)
- 单一继承与多重继承
- Silverlight编译错误,提示某文件不是项目的一部分或"生成操作"没有设置为"资源"
- Linux 用Sendmail架设Mail服务器
- 解决Only the original thread that created a view hierarchy can touch its views
- near far huge
- Hadoop使用常见问题以及解决方法(转载)
- Choose a destination with a supported architecture in order to run on this device
- wordpress tittle 烦人的书名号
- Oracle学习笔记之二SQL更新数据和一些常用函数
- YUM常用命令介绍
- 网络编程socket之close与shutdown函数
- HTML5背后的故事