错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用
在跟随教程学习到显示web页面的html源码时报错:Only the original thread that created a view hierarchy can touch its views,通过网上查找资料得知:
android中相关的view和控件不是线程安全的,必须单独做处理。如果要更新视图,必须在主线程中更新,不可以在子线程中执行更新的操作。
既然这样,我们就可以通过Handler对象实现对UI的更新。
Handler的官方描述:
A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue
.Handler的使用场合:
1、 to schedule messages and runnables to be executed as some point in the future;
安排messages和runnables在将来的某个时间点执行。
2、 to enqueue an action to be performed on a different thread than your own.
将action入队以备在一个不同的线程中执行。即可以实现线程间通信。比如当你创建子线程时,你可以再你的子线程中拿到父线程中创建的Handler对象,就可以通过该对象向父线程的消息队列发送消息了。由于Android要求在UI线程中更新界面,因此,可以通过该方法在其它线程中更新界面。
通过Handler更新UI实例:
步骤:
1、创建Handler对象(此处创建于主线程中便于更新UI)。
2、构建Runnable对象,在Runnable中更新界面。
3、在子线程的run方法中向UI线程post,runnable对象来更新UI。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_html);
phtml=(EditText) findViewById(R.id.phtml);
button1=(Button) findViewById(R.id.check_html);
ihtml=(TextView) findViewById(R.id.ihtml);
//创建属于主线程的handler
handler=new Handler();
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
new Thread(){
public void run(){
try {
String path=phtml.getText().toString();
shtml=HtmlService.getHtml(path);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.post(runnableUi);
}
}.start();
//new HtmlSourceAsync(ihtml).execute(path);
}
});
}
// 构建Runnable对象,在runnable中更新界面
Runnable runnableUi=new Runnable(){
@Override
public void run() {
//更新界面
ihtml.setText(shtml);
}
};
文章部分引自:http://blog.csdn.net/djx123456/article/details/6325983
错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用的更多相关文章
- andriod 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用
package com.example.yanlei.myapplication; import android.media.MediaMetadataRetriever; import androi ...
- 开发错误记录1:解决:Only the original thread that created a view hierarchy can touch its views.
今天在项目中要使用圆角头像,导入开源 CircleImageView ,然后setImageBitmap()时 运行时就会发现,它会报一个致命性的异常:: · ERROR/AndroidRuntime ...
- Only the original thread that created a view hierarchy can touch its views
在调试软件的时候出现如下的错误: 01-05 20:53:36.492: E/ZZShip(2043): android.view.ViewRootImpl$CalledFromWrongThread ...
- Android: Only the original thread that created a view hierarchy can touch its views 异常
最近自己再写一个小项目练手,创建一个线程从网络获取数据然后显示在 recyclerView 上.写好后发现页面能够显示,但是有时候会把请求的数据显示过来,有时候不会.点开 android monito ...
- 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.
在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...
- Only the original thread that created a view hierarchy can touch its views解决办法
这周操作系统作业布置了一个作业,内容是做个小软件,来模拟消费者生产者问题,作业实现起来不来,因为之前写过这个算法,所以关键步骤就是在消费和生产的时候更新缓存区的UI控件就行,之后问题就来了,出现了标题 ...
- 子线程调用invalidate()产生“Only the original thread that created a view hierarchy can touch its views.”原因分析
目录 1.异常出处 2.从View.invalidate()方法开始分析 3.ViewRootImpl如何与View进行关联:从Activity的setContentView开始分析 3.1 最顶层的 ...
- 解决Only the original thread that created a view hierarchy can touch its views
这种异常出现在子线程中处理UI操作产生的异常,将UI操作放在主线程中就OK了
- "Only the original thread that created a view hierarchy can touch its views.” 解决方法
这个主要总是,开启的线程和 UI 线程(主线程)不是同一个线程.可以Runnable方式避免,如下例所示就可以解决这个问题了. public static void updateText(Activi ...
随机推荐
- webpack面试题(转载)
1:webpack打包原理 把所有依赖打包成一个bundle.js文件,通过代码分割成单元片段并按需加载. 2:webpack的优势 (1) webpack是以commonJS的形式来书写 ...
- 实现webpack的实时打包构建
1. 由于每次重新修改代码之后,都需要手动运行webpack打包的命令,比较麻烦,所以使用`webpack-dev-server`来实现代码实时打包编译,当修改代码之后,会自动进行打包构建.2. 运行 ...
- Python numpy插入、读取至postgreSQL数据库中bytea类型字段
安装psycopg2模块,此模块用于连接PostgreSQL数据库 pip install psycopg2 # -*- coding: utf-8 -*- import psycopg2 impo ...
- egon消失的一天,空虚寂寞冷,苑模块的时间
一.时间模块time python有三种表达时间的形式:时间戳.格式化字符串输出和元组. 时间戳:从1970年1月1日00:00:00开始按秒计算的偏移量,返回值是一个float型. 格式化字符串输出 ...
- 大数据数据库HBase(一)——架构原理
一.HBase简介 1.1.Hadoop生态系统 1.2.非关系型数据库知识面扩展 Cassandra hbase mongodb Couchdb,文件存储数据库 Neo4j非关系型图数据库 1.3 ...
- Comet OJ - Contest #3 B -棋盘 (思维+分类讨论)
题目描述 小猫有一个 2\times N2×N 的棋盘,每一个格子放着一个黑棋子或白棋子. 小熊觉得小猫的棋盘不够好看,想要把棋盘上的一部分白棋子替换成黑棋子,使得所有黑棋子都能够在仅允许上下左右四个 ...
- 树莓派上固定ip
sudo nano /etc/dhcpcd.conf interface eth0 static ip_address=192.168.123.99/24 static routers=192.168 ...
- C#基础知识之DirectorySearcher 类
活动目录(Active Directory)是从一个数据存储开始的,它采用了类似Exchange Server的数据存储,所以被称为Extensible Storage Service (ESS).其 ...
- bzoj3091 城市旅行 LCT + 区间合并
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3091 题解 调了整个晚自习才调出来的问题. 乍一看是个 LCT 板子题. 再看一眼还是个 LC ...
- python-globals()、locals()的使用
globals() 函数返回一个全局变量的字典,包括所有导入的变量locals() 函数返回一个当前位置的所有局部变量的字典print(globals())print(locals()) global ...