简体   繁体   English

无法从Runnable转换为Thread

[英]cannot convert from Runnable to Thread

I get this error message "cannot convert from Runnable to Thread" This comes up for the Threat T = new Runnable(r); 我收到此错误消息“无法从Runnable转换为线程”这出现了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();

You have a wrong line in your code 您的代码中有一行错误的行

Change 更改

Thread T = new Runnable(r);

to

Thread T = new Thread(r);

Thread实现了Runnable ,而不是相反。

Sherif's right. 谢里夫是对的。 I'd also recommend some code cleanup, to avoid all the runnables and threads you've got going. 我还建议一些代码清理,以避免你已经运行的所有runnables和线程。 Just use a handler to do your update, and request another update 30 seconds after the current update. 只需使用处理程序进行更新,并在当前更新后30秒请求另一次更新。 This will be handled on the UI thread. 这将在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);
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM