在android中的线程内设置TextView值
我在流动的情况下发生了崩溃。我按以下方式运行线程:
Thread t = new Thread(){
public void run() {
text.setText("hello");
}
};
t.start;
如果我尝试在 xml 中设置 TextView
的值(对文本的引用已可用),则会发生崩溃。
我是否在做一些根本性错误的事情?哪里出错了还请指出。
I am getting a crash under the fllowing circumstances. I am running a thread in the following way:
Thread t = new Thread(){
public void run() {
text.setText("hello");
}
};
t.start;
The crash occurs if I try to set the value of a TextView
in my xml, (the reference to text is already available).
Am I doing something fundamentally wrong? Kindly point out where am going wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只能从 UI 线程访问用户界面组件。
Android 有一些东西可以让这个变得简单,例如方法 runOnUiThread 和类 异步任务。
如需更多阅读,请参阅无痛线程和进程和线程。
You can only access user interface components from the UI thread.
Android has a few things to make this easy, such as the method runOnUiThread and the class AsyncTask.
For more reading see Painless Threading and Processes and Threads in the Android documentation.
您应该仅在 UI 线程上访问 android ui 工具包小部件。阅读http://developer.android.com/resources/articles/painless-threading。 html。
You should access android ui toolkit widgets only on the UI thread. Read http://developer.android.com/resources/articles/painless-threading.html.
使用 Handler 类并检查它是否有更多相关方法
use Handler class and check it for more relevant methods