无法从 Runnable 转换为 Thread
我收到此错误消息“无法从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码中有错误的行
更改
为
You have a wrong line in your code
Change
to
Thread
实现Runnable
,而不是相反。Thread
implementsRunnable
, not the other way round.谢里夫是对的。我还建议进行一些代码清理,以避免出现所有可运行对象和线程。只需使用处理程序进行更新,并在当前更新后 30 秒请求另一次更新。这将在 UI 线程上处理。
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.