如何从主题textView.setText? [英] How to textView.setText from Thread?
本文介绍了如何从主题textView.setText?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从线程设置文字的TextView。所有code在创建OnCreate中()
I need to set text to textView from thread. All code is created in oncreate()
像
public TextView pc;
oncreate(..) {
setContentView(R.layout.main);
pc = new TextView(context);
Thread t =new Thread() {
public void run() {
pc.setText("test");
}};
t.start();
这崩溃我的应用程序。如何从主题设置文本?
This crashes my app. How can I set text from thread?
推荐答案
使用处理器:
public TextView pc;
Handler handler = new Handler();
oncreate(..) {
setContentView(R.layout.main);
pc = new TextView(context);
Thread t =new Thread(){
public void run() {
handler.post(new Runnable() {
public void run() {
pc.setText("test");
}
});
}
}};
t.start();
}
但是你有另外一个问题。 PC
指向一个观点,是不是你的层次上。你可能想使用 findViewById
与TextView的在布局的ID。
But you have another problem. pc
points to a view that is not part of your hierarchy. You probably want to use findViewById
with the id of the TextView in your layout.
这篇关于如何从主题textView.setText?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文