安卓問題總結二(更新UI出現的問題)

來源:互聯網
上載者:User

標籤:

今天測試視頻資訊用戶端,跟新UI是 LogCat 控制台出現了這樣的錯誤:Only the original thread that created a view hierarchy can touch its views.  網上搜了一下才發現:原來android中相關的view和控制項不是安全執行緒的,我們必須單獨做處理。所以這裡再次用到了Handler這個類來操作。到這裡我才認識到(後知後覺) 原來自己前面已經使用過這種方式來跟新UI的操作。沒想到這裡就給忘了。這裡記錄下 省以後再忘記:

代碼如下:

//Handler的使用跟新UI

final Handler handler = new Handler(){

public void handleMessage(Message msg){

switch (msg.what) {

case 0:

listView.setAdapter(adapter);

break;

default:

Toast.makeText(getApplicationContext(), R.string.error, 1)

.show();

break;

}

}

};

//使用線程載入資料

new Thread(new Runnable() {

@Override

public void run() {

 try {

List<Video> videos = VideoService.getAll();//需修改成你原生Http請求路徑

List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();

for(Video video : videos){

System.out.println("---------------------------------------==="+video);

HashMap<String, Object> item = new HashMap<String, Object>();

item.put("id", video.getId());

item.put("name", video.getName());

item.put("time", getResources().getString(R.string.timelength)

+ video.getTimelength()+ getResources().getString(R.string.min));

data.add(item);

}

adapter = new SimpleAdapter(getApplicationContext(), data, R.layout.items,

new String[]{"name", "time"}, new int[]{R.id.name, R.id.time});

handler.sendEmptyMessage(0);

} catch (Exception e) {

e.printStackTrace();

}

}

}).start();

最後在來說下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可以分發Message對象和Runnable對象到主線程中, 每個Handler執行個體,都會綁定到建立他的線程中(一般是位於主線程),
        它有兩個作用: (1):  安排訊息或Runnable 在某個主線程中某個地方執行, (2)安排一個動作在不同的線程中執行
        Handler中分發訊息的一些方法
        post(Runnable)
        postAtTime(Runnable,long)
        postDelayed(Runnable long)
        sendEmptyMessage(int)
        sendMessage(Message)
        sendMessageAtTime(Message,long)
        sendMessageDelayed(Message,long)
        以上post類方法允許你排列一個Runnable對象到主線程隊列中,
        sendMessage類方法, 允許你安排一個帶資料的Message對象到隊列中,等待更新.

通過Handler更新UI執行個體:(這個是別人寫的這裡自己留著做個參考)

步驟:

1、建立Handler對象(此處建立於主線程中便於更新UI)。

2、構建Runnable對象,在Runnable中更新介面。

3、在子線程的run方法中向UI線程post,runnable對象來更新UI。



安卓問題總結二(更新UI出現的問題)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.