无法从 Runnable 转换为 Thread

发布于 2024-11-27 22:01:25 字数 977 浏览 1 评论 0原文

我收到此错误消息“无法从 Runnable 转换为线程” 这是针对威胁 T = new Runnable(r); 出现的

这是我的代码...

final String[] texts = new String[]{player, player11, player111}; //etc
            final Runnable r = new Runnable(){
                public void run(){
                    for(final int i=0;i<texts.length;i++){
                        synchronized(this){
                            wait(30000); //wait 30 seconds before changing text
                        }
                        //to change the textView you must run code on UI Thread so:
                        runOnUiThread(new Runnable(){
                            public void run(){
                                TextView t = (TextView) findViewById(R.id.textView1);
                                t.setText(texts[i]);
                            }
                        });
                    }
                }
            };
            Thread T = new Runnable(r);
            T.start();

I get this error message "cannot convert from Runnable to Thread" This comes up for the Threat T = new Runnable(r);

Here is my code...

final String[] texts = new String[]{player, player11, player111}; //etc
            final Runnable r = new Runnable(){
                public void run(){
                    for(final int i=0;i<texts.length;i++){
                        synchronized(this){
                            wait(30000); //wait 30 seconds before changing text
                        }
                        //to change the textView you must run code on UI Thread so:
                        runOnUiThread(new Runnable(){
                            public void run(){
                                TextView t = (TextView) findViewById(R.id.textView1);
                                t.setText(texts[i]);
                            }
                        });
                    }
                }
            };
            Thread T = new Runnable(r);
            T.start();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

↙厌世 2024-12-04 22:01:26

您的代码中有错误的行

更改

Thread T = new Runnable(r);

Thread T = new Thread(r);

You have a wrong line in your code

Change

Thread T = new Runnable(r);

to

Thread T = new Thread(r);
渡你暖光 2024-12-04 22:01:26

Thread 实现 Runnable,而不是相反。

Thread implements Runnable, not the other way round.

暮凉 2024-12-04 22:01:26

谢里夫是对的。我还建议进行一些代码清理,以避免出现所有可运行对象和线程。只需使用处理程序进行更新,并在当前更新后 30 秒请求另一次更新。这将在 UI 线程上处理。

TextView t;
Handler handler;
int count = 0;

@Override
public void onCreate(Bundle bundle)
{
    t = (TextView) findViewById(R.id.textView1);
    Handler handler = new Handler();
    handler.post(uiUpdater);
}

Runnable uiUpdater = new Runnable()
{
    @Override
    public void run()
    {
        count = (count + 1) % texts.length;
        t.setText(texts[count]);

        handler.removeCallbacks(uiUpdater);
        handler.postDelayed(uiUpdater, 30000);
    }
};

Sherif's right. I'd also recommend some code cleanup, to avoid all the runnables and threads you've got going. Just use a handler to do your update, and request another update 30 seconds after the current update. This will be handled on the UI thread.

TextView t;
Handler handler;
int count = 0;

@Override
public void onCreate(Bundle bundle)
{
    t = (TextView) findViewById(R.id.textView1);
    Handler handler = new Handler();
    handler.post(uiUpdater);
}

Runnable uiUpdater = new Runnable()
{
    @Override
    public void run()
    {
        count = (count + 1) % texts.length;
        t.setText(texts[count]);

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