2011-08-05 44 views
0

我得到這個錯誤消息「無法從Runnable接口轉換主題」這出現了威脅T =新的Runnable(R);不能從Runnable接口轉換主題

這裏是我的代碼...

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

回答

2

你有錯線在你的代碼

變化

Thread T = new Runnable(r); 

Thread T = new Thread(r); 
0

Thread實現Runnable,而不是反過來。

+0

因此,如何將我得到修復呢?我是一個業餘愛好者 –

0

謝里夫的權利。我還推薦一些代碼清理,以避免所有可運行和線程。只需使用處理程序進行更新,並在當前更新後的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); 
    } 
};