无法从Runnable转换为Thread

时间:2011-08-05 00:44:13

标签: android multithreading textview delay runnable

我收到此错误消息“无法从Runnable转换为线程”这样就出现了Threat 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();

3 个答案:

答案 0 :(得分:2)

您的代码中有错误的行

更改

Thread T = new Runnable(r);

Thread T = new Thread(r);

答案 1 :(得分:0)

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

答案 2 :(得分:0)

谢里夫是对的。我还建议一些代码清理,以避免你已经运行的所有runnables和线程。只需使用处理程序进行更新,并在当前更新后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);
    }
};