开发者

cannot convert from Runnable to Thread

开发者 https://www.devze.com 2023-03-26 05:13 出处:网络
I get this error message \"cannot convert fr开发者_如何学运维om Runnable to Thread\"This comes up for the Threat T = new Runnable(r);

I get this error message "cannot convert fr开发者_如何学运维om 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();


You have a wrong line in your code

Change

Thread T = new Runnable(r);

to

Thread T = new Thread(r);


Thread implements Runnable, not the other way round.


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);
    }
};
0

精彩评论

暂无评论...
验证码 换一张
取 消