直接在UI线程中开启子线程来更新TextView显示的内容,运行程序我们会发现,如下错 误:android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.翻译过来就是:只有创建这个控件的线程才能去更新该控件的内容。
所有的UI线程要去负责View的创建并且维护它,例如更新冒个TextView的显示,都必须在主线程中去做,我们不能直接在UI线程中去创建子线程,要利用消息机制:handler,如下就是handler的简单工作原理图:
既然android给我们提供了Handler机制来解决这样的问题,请看如下代码:
01 |
public class HandlerTestActivity extends Activity { |
02 |
private TextView tv; |
03 |
private static final int UPDATE = 0 ; |
04 |
private Handler handler = new Handler() { |
05 |
|
06 |
@Override |
07 |
public void handleMessage(Message msg) { |
08 |
// TODO 接收消息并且去更新UI线程上的控件内容 |
09 |
if (msg.what == UPDATE) { |
10 |
// Bundle b = msg.getData(); |
11 |
// tv.setText(b.getString("num")); |
12 |
tv.setText(String.valueOf(msg.obj)); |
13 |
} |
14 |
super .handleMessage(msg); |
15 |
} |
16 |
}; |
17 |
|
18 |
/** Called when the activity is first created. */ |
19 |
@Override |
20 |
public void onCreate(Bundle savedInstanceState) { |
21 |
super .onCreate(savedInstanceState); |
22 |
setContentView(R.layout.main); |
23 |
tv = (TextView) findViewById(R.id.tv); |
24 |
|
25 |
new Thread() { |
26 |
@Override |
27 |
public void run() { |
28 |
// TODO 子线程中通过handler发送消息给handler接收,由handler去更新TextView的值 |
29 |
try { |
30 |
for ( int i = 0 ; i < 100 ; i++) { |
31 |
Thread.sleep( 500 ); |
32 |
Message msg = new Message(); |
33 |
msg.what = UPDATE; |
34 |
// Bundle b = new Bundle(); |
35 |
// b.putString("num", "更新后的值:" + i); |
36 |
// msg.setData(b); |
37 |
msg.obj = "更新后的值:" + i; |
38 |
handler.sendMessage(msg); |
39 |
} |
40 |
} catch (InterruptedException e) { |
41 |
e.printStackTrace(); |
42 |
} |
43 |
} |
44 |
}.start(); |
45 |
} |
46 |
|
47 |
} |
我们就通过Handler机制来处理了子线程去更新UI线程控件问题,Andrid开发中要经常用到这种机制。