在android中的线程内设置TextView值

发布于 2024-11-18 10:10:33 字数 270 浏览 2 评论 0原文

我在流动的情况下发生了崩溃。我按以下方式运行线程:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小嗲 2024-11-25 10:10:33

您只能从 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.

与酒说心事 2024-11-25 10:10:33

您应该仅在 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.

独夜无伴 2024-11-25 10:10:33

使用 Handler 类并检查它是否有更多相关方法

Handler mHandler;
 mHandler=new Handler(){
hdandleMessage(Message what){

text.setText("hello");

}

};
Thread t = new Thread(){
    public void run()
    {

           mHandler.sendEmptyMessage(int what)

        }
};
t.start;

use Handler class and check it for more relevant methods

Handler mHandler;
 mHandler=new Handler(){
hdandleMessage(Message what){

text.setText("hello");

}

};
Thread t = new Thread(){
    public void run()
    {

           mHandler.sendEmptyMessage(int what)

        }
};
t.start;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文