我需要从线程设置文字的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.