android子线程控制UI线程,以防:Only the original thread that created a view hierarchy can touch its views.
来源:互联网 发布:子域名算跨域吗 编辑:程序博客网 时间:2024/05/24 01:50
近段时间比较空闲,也有时间让我自己冲冲电~
今天和大家分享的是android 中子线程控制UI线程的几个常用方法,因为android是单线程执行的,如果你在子线程中操作有UI线程就会报错了以下错误:Only the original thread that created a view hierarchy can touch its views.
第一种:(简单明了)
XXXActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
//do something
setTitle("控制UI线程")
}
});
第二种: Runnable + Handler.postDelayed(runnable,time)
private Handler handler = new Handler(); private Runnable myRunnable= new Runnable() { public void run() { if (run) { handler.postDelayed(this, 1000); count++; } tvCounter.setText("Count: " + count); } };
然后在其他地方调用
handler.post(myRunnable);
handler.post(myRunnable,time);
第三种:Handler(子线程调用Handler的handle.sendMessage(msg);Handler handle = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
mTextView.setText(来自网络的文字);
}
};
class MyThread extends Thread {
@Override
public void run() {
// 耗时操作
loadNetWork();
Message msg = new Message();
handle.sendMessage(msg);
super.run();
}
}
第四种:AsyncTask
(这块在接下去的一篇文章详细的描述)
- android子线程控制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
- 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
- android studio 线程安全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
- Android 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: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】错误信息 Only the original thread that created a view hierarchy can touch its views
- ERROR:Only the original thread that created a view hierarchy can touch its views. Android更新UI
- 在工作线程中处理UI线程报错: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.
- 在工作线程中处理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错误解决
- 新系统如何确定性能测试目标
- 用fopen打开Qstring文件;想获取Qstring最后几位字符
- NYOJ 53 不高兴的小明
- 简易命令行界面的C/S聊天室
- UILabel字体颜色和大小
- android子线程控制UI线程,以防:Only the original thread that created a view hierarchy can touch its views.
- AngularJS笔记
- Java调用动态库方法说明
- 在tomcat下部署drools 6.2.0 workbench
- 中国人的羊性和日本人的狼性
- [Cocos2d-x v3.x官方文档]Cocos2d-x 3.0坐标系详解
- 中文化和国际化问题权威解析之一:字符编码发展历程
- 贝叶斯的JAVA分类器实现
- iOS多视图控制器跳转方法