錯誤: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. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章