Andrew Hill
Andrew Hill

Reputation: 25

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

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();

Upvotes: 0

Views: 2934

Answers (3)

Josh
Josh

Reputation: 10738

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

Upvotes: 0

Sherif elKhatib
Sherif elKhatib

Reputation: 45942

You have a wrong line in your code

Change

Thread T = new Runnable(r);

to

Thread T = new Thread(r);

Upvotes: 2

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272657

Thread implements Runnable, not the other way round.

Upvotes: 0

Related Questions