5

不知怎的,這是行不通的,據我應該是這樣的:?runOnUiThread(新的Runnable(){標點(標記)問題

public void Splash(){ 
    Timer timer= new Timer(); 

    timer.schedule(new TimerTask(){ 

    MexGame.this.runOnUiThread(new Runnable() { 

     public void run(){ 
     SplashImage.setImageDrawable(aktieknop); 
     } //Closes run() 

     }); //Closes runOnUiThread((){}) 

    },SplashTime); //Closes the Timeratask((){}) 

} //closes Splash() 

任何人在那裏我失去了一些東西什麼想法

正式講話 我知道傻問題,或者也許我做的事情是不可能的,但我嘗試了所有的邏輯可能性,所以可能失去了一些東西還是我試圖做一些事情,是不可能的。 能否請你幫我出了 我正在嘗試使用followi NG代碼,但給的令牌問題:

Timer timer= new Timer(); 
    timer.schedule(new TimerTask(){ 

    runOnUiThread(new Runnable() { 

     public void run(){ 
     SplashImage.setImageDrawable(aktieknop);} 

     });} 

    },SplashTime); 

如果我阻止了它崩潰了,因爲我在努力適應從另一個線程的UI,但至少沒有令牌的問題,任何人任何想法runOnUiThread?:

Timer timer= new Timer(); 


    timer.schedule(new TimerTask(){ 

// runOnUiThread(new Runnable() { 

     public void run(){ 
     SplashImage.setImageDrawable(aktieknop);} 

    // });} 

    },SplashTime); 
+0

你試過用Activity_name.this.runOnUiThread(...)嗎? –

+0

嗨Lalit,給出了同樣的問題,日食給了我應該添加或刪除的評論;和} {等。沒有東西阻止了我得到的評論在最後與timer.schedule和SplashTime。看起來Eclipse在添加runOnUiThread時無法識別Timer Task .. – Diego

回答

9

無論是TimerTask的和了Runnable要求你實現一個run方法,所以你需要兩個run方法。

如果您將Runnable的構造與TimerTask的構造分開,那麼您的代碼將更易於閱讀。

final Runnable setImageRunnable = new Runnable() { 
     public void run() { 
      splashImage.setImageDrawable(aktieknop); 
     } 
    }; 

    TimerTask task = new TimerTask(){ 
     public void run() { 
      getActivity().runOnUiThread(setImageRunnable); 
     } 
    }; 

    Timer timer = new Timer(); 
    timer.schedule(task, splashTime); 
+0

+,我錯過了'Timer(run)()'缺少'TimerTask' – Vladimir

1

您在SplashTime之前有多餘的「}」。你已經評論了一個打開的「{」和兩個關閉的「}」,所以你的原始代碼有一個不需要的「}」。

Timer timer= new Timer(); 
timer.schedule(new TimerTask(){ 
     runOnUiThread(new Runnable() { 
      public void run(){ 
       SplashImage.setImageDrawable(aktieknop); 
      } //closes run(){}   
     });  //closes runOnUiThread(Runnable(){ }); 
    },   //closes TimerTask(){} 
    SplashTime);