Only the original thread that created a view hierarchy can touch its views——Handler的使用
来源:互联网 发布:淘宝名表代购可靠吗 编辑:程序博客网 时间:2024/05/25 21:36
今天写了一个更新UI的小例子,没想到出了log打印了这样一个错误:Only the original thread that created a view hierarchy can touch its views。goolgle了一下找到了原因。
原来android中相关的view和控件不是线程安全的,我们必须单独做处理。这里借此引出Handler的使用。
通过Handler更新UI实例:
步骤:
1、创建Handler对象(此处创建于主线程中便于更新UI)。
2、构建Runnable对象,在Runnable中更新界面。
3、在子线程的run方法中向UI线程post,runnable对象来更新UI。
package djx.android;import djx.downLoad.DownFiles;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class downLoadPractice extends Activity {private Button button_submit=null;private TextView textView=null;private String content=null;private Handler handler=null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //创建属于主线程的handler handler=new Handler(); button_submit=(Button)findViewById(R.id.button_submit); textView=(TextView)findViewById(R.id.textView); button_submit.setOnClickListener(new submitOnClieckListener()); } //为按钮添加监听器 class submitOnClieckListener implements OnClickListener{@Overridepublic void onClick(View v) {//本地机器部署为服务器,从本地下载a.txt文件内容在textView上显示final DownFiles df=new DownFiles("http://192.168.75.1:8080/downLoadServer/a.txt");textView.setText("正在加载......");new Thread(){public void run(){content=df.downLoadFiles();handler.post(runnableUi); }}.start();} } // 构建Runnable对象,在runnable中更新界面 Runnable runnableUi=new Runnable(){@Overridepublic void run() {//更新界面textView.setText("the Content is:"+content);} }; }
0 0
- 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用
- Only the original thread that created a view hierarchy can touch its views——Handler的使用
- Only the original thread that created a view hierarchy can touch its views——Handler的使用
- 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用
- Only the original thread that created a view hierarchy can touch its views——Handler的使用
- 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用
- 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用
- 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用
- 错误:Only the original thread that created a view hierarchy can touch its views——Handler的深入解析
- 错误:Only the original thread that created a view hierarchy can touch its views——Handler的深入解析
- 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使
- 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的解决办法
- Entity Framework 教程
- Career Path(职业前景人生规划)的几点见解
- android的res/raw和assets文件夹
- New Beginning
- Windows8和Fedora19双系统安装(U盘方式)及Fedora常用软件安装
- Only the original thread that created a view hierarchy can touch its views——Handler的使用
- OpenCart之账户(Account)模块教程
- Qt5.2 for Android 配置及部署到手机运行
- 18数据库设计
- 编译器工作流程原理
- 第16周项目8-有相同数字?
- 数组中的相同数字的处理
- su和su -的区别
- 修复被破坏了的linux文件系统分区表