错误:Only the original thread that created a view hierarchy can touch its views——Handler的深入解析

这个错误很常见,基本上写线程操作都遇到过这个错误。根本原因是view控件的线程安全问题,通俗点讲就是所有的更新UI操作都需要在主线程(也就是UI线程中完成),而不能在新开的子线程中操作。

基本思路:既然子线程需要更新UI,但子线程自身又不能完成任务,所以只能通过建立一个通信机制,当子线程需要更新UI时,发消息通知主线程并将更新UI的任务post给主线程,让主线程来完成分内的UI更新操作。这个机制是什么呢?就是Handler。Handler 从属于谁?当然是主线程。每个线程都有自己的handler,来处理自己的消息队列,只不过平时写单线程操作,系统会缺省调用一个handler,对开发者透明。当多线程操作需要线程间通信时,handler才会被程序猿们显示调用。

下面这两个例子是更新UI时主线程和子线程通信的例子,因为控件不是线程安全的,所以子线程中涉及到的更新UI操作全都写入runnable对象、通过主线程的handler来post给UI。


第一个例子,从网上找的,总结的比较到位。谢谢原作者的辛勤总结,转载地址标注于下。

原文转自 http://blog.csdn.net/djx123456/article/details/6325983

今天写了一个更新UI的小例子,没想到出了log打印了这样一个错误:Only the original thread that created a view hierarchy can touch its views。goolgle了一下找到了原因。

原来android中相关的view和控件不是线程安全的,我们必须单独做处理。这里借此引出Handler的使用。

 

  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。

 

详细代码如下:

[java] view plaincopy
  1. package djx.android;  
  2.   
  3. import djx.downLoad.DownFiles;  
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.os.Handler;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.TextView;  
  11.   
  12. public class downLoadPractice extends Activity {  
  13.     private Button button_submit=null;  
  14.     private TextView textView=null;  
  15.     private String content=null;  
  16.     private Handler handler=null;  
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         //创建属于主线程的handler  
  23.         handler=new Handler();  
  24.           
  25.         button_submit=(Button)findViewById(R.id.button_submit);  
  26.         textView=(TextView)findViewById(R.id.textView);  
  27.         button_submit.setOnClickListener(new submitOnClieckListener());  
  28.     }  
  29.     //为按钮添加监听器  
  30.     class submitOnClieckListener implements OnClickListener{  
  31.         @Override  
  32.         public void onClick(View v) {  
  33. //本地机器部署为服务器,从本地下载a.txt文件内容在textView上显示           
  34.             final DownFiles df=new DownFiles("http://192.168.75.1:8080/downLoadServer/a.txt");  
  35.             textView.setText("正在加载......");  
  36.             new Thread(){  
  37.                 public void run(){    
  38.                     content=df.downLoadFiles();       
  39.                     handler.post(runnableUi);   
  40.                     }                     
  41.             }.start();                        
  42.         }  
  43.           
  44.     }   
  45.   
  46.    // 构建Runnable对象,在runnable中更新界面  
  47.     Runnable   runnableUi=new  Runnable(){  
  48.         @Override  
  49.         public void run() {  
  50.             //更新界面  
  51.             textView.setText("the Content is:"+content);  
  52.         }  
  53.           
  54.     };  
  55.           
  56.       
  57. }  

第二个例子,这两天在实验室写的人防工程的应用程序,包括动态新闻、巡查信息等是需要更新UI的,以下把动态新闻更新UI的代码贴出来。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * 动态新闻 
  3.  *  
  4.  * @author GloryZSG 
  5.  */  
  6. public class NewsDetail extends Activity {  
  7.     private TextView bar;  
  8.     private TextView noticename;  
  9.     private TextView noticeauthor;  
  10.     private TextView noticetime;  
  11.     private TextView noticeinfo;  
  12.     private TextView newsImageText;  
  13.     private ImageView newsImage;  
  14.     private String imgUrl;  
  15.     private Bitmap bm;  
  16.     private Handler handler;  
  17.   
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         this.setContentView(R.layout.news_details);  
  22.           
  23.         handler = new Handler();  
  24.         HashMap<String, Object> map = new HashMap<String, Object>();  
  25.         try {  
  26.             Bundle bundle = getIntent().getExtras();  
  27.             Serializable data = bundle.getSerializable("taskinfo");  
  28.             if (data != null) {  
  29.                 map = (HashMap<String, Object>) data;  
  30.             } else {  
  31.                 return;  
  32.             }  
  33.         } catch (Exception e) {  
  34.             e.printStackTrace();  
  35.         }  
  36.           
  37.         // (2014.5.7第二种方法)通过服务器返回的图片url,再次向服务器请求,添加动态新闻图片  
  38.         noticename = (TextView) findViewById(R.id.noticename);  
  39.         noticename.setText("标题:" + map.get("title").toString());  
  40.         noticeauthor = (TextView) findViewById(R.id.noticeauthor);  
  41.         noticeauthor.setText("作者:" + map.get("reporterUser").toString());  
  42.         noticetime = (TextView) findViewById(R.id.noticetime);  
  43.         noticetime.setText("时间:" + map.get("reportTime").toString());  
  44.         noticeinfo = (TextView) findViewById(R.id.noticeinfo);  
  45.         noticeinfo.setText("动态新闻详情:" + map.get("detail").toString());  
  46.         newsImageText = (TextView) findViewById(R.id.imgLoadingText);  
  47.         // 获取图片url  
  48.         imgUrl = map.get("activityPhoto").toString();  
  49.           
  50.         new Thread() {  
  51.             public void run() {  
  52.                 try {  
  53.                     URL url;  
  54.                     url = new URL(imgUrl);  
  55.                     HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  56.                     InputStream is = conn.getInputStream();  
  57.                     bm = BitmapFactory.decodeStream(is);  
  58.                 } catch (MalformedURLException e) {  
  59.                     // TODO Auto-generated catch block  
  60.                     e.printStackTrace();  
  61.                 } catch (IOException e) {  
  62.                     e.printStackTrace();  
  63.                 }  
  64.                 handler.post(runnableUI);  
  65.             }  
  66.         }.start();  
  67.     }  
  68.       
  69.     /** 
  70.      * 读取图片的子线程post给主线程的runnable对象,内含各种更新UI的操作 
  71.      *  
  72.      * @author GloryZSG 
  73.      */  
  74.     Runnable runnableUI = new Runnable() {  
  75.             public void run() {  
  76.                 // (2014.5.1第一种方法)通过服务器返回的图片url,再次向服务器请求,添加动态新闻图片  
  77.                 // 读取Bitmap图片  
  78.                 // 加载到布局文件中  
  79.                 newsImageText.setVisibility(View.GONE);  
  80.                 newsImage = (ImageView) findViewById(R.id.imageView);  
  81.                 newsImage.setImageBitmap(bm);  
  82.   
  83.             }  
  84.         };  
  85.   
  86. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章