這個錯誤很常見,基本上寫線程操作都遇到過這個錯誤。根本原因是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。
詳細代碼如下:
- 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{
- @Override
- public 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(){
- @Override
- public void run() {
- //更新界面
- textView.setText("the Content is:"+content);
- }
- };
- }
第二個例子,這兩天在實驗室寫的人防工程的應用程序,包括動態新聞、巡查信息等是需要更新UI的,以下把動態新聞更新UI的代碼貼出來。
- /**
- * 動態新聞
- *
- * @author GloryZSG
- */
- public class NewsDetail extends Activity {
- private TextView bar;
- private TextView noticename;
- private TextView noticeauthor;
- private TextView noticetime;
- private TextView noticeinfo;
- private TextView newsImageText;
- private ImageView newsImage;
- private String imgUrl;
- private Bitmap bm;
- private Handler handler;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.news_details);
- handler = new Handler();
- HashMap<String, Object> map = new HashMap<String, Object>();
- try {
- Bundle bundle = getIntent().getExtras();
- Serializable data = bundle.getSerializable("taskinfo");
- if (data != null) {
- map = (HashMap<String, Object>) data;
- } else {
- return;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- // (2014.5.7第二種方法)通過服務器返回的圖片url,再次向服務器請求,添加動態新聞圖片
- noticename = (TextView) findViewById(R.id.noticename);
- noticename.setText("標題:" + map.get("title").toString());
- noticeauthor = (TextView) findViewById(R.id.noticeauthor);
- noticeauthor.setText("作者:" + map.get("reporterUser").toString());
- noticetime = (TextView) findViewById(R.id.noticetime);
- noticetime.setText("時間:" + map.get("reportTime").toString());
- noticeinfo = (TextView) findViewById(R.id.noticeinfo);
- noticeinfo.setText("動態新聞詳情:" + map.get("detail").toString());
- newsImageText = (TextView) findViewById(R.id.imgLoadingText);
- // 獲取圖片url
- imgUrl = map.get("activityPhoto").toString();
- new Thread() {
- public void run() {
- try {
- URL url;
- url = new URL(imgUrl);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- InputStream is = conn.getInputStream();
- bm = BitmapFactory.decodeStream(is);
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- handler.post(runnableUI);
- }
- }.start();
- }
- /**
- * 讀取圖片的子線程post給主線程的runnable對象,內含各種更新UI的操作
- *
- * @author GloryZSG
- */
- Runnable runnableUI = new Runnable() {
- public void run() {
- // (2014.5.1第一種方法)通過服務器返回的圖片url,再次向服務器請求,添加動態新聞圖片
- // 讀取Bitmap圖片
- // 加載到佈局文件中
- newsImageText.setVisibility(View.GONE);
- newsImage = (ImageView) findViewById(R.id.imageView);
- newsImage.setImageBitmap(bm);
- }
- };
- }