2014-04-10 51 views
0

我有,我有一個Runnable哪些更新搜索條和currentposition TextView的每100毫秒更新的TextView導致活動停止

private Runnable mUpdateTimeTask = new Runnable() { 
     public void run() { 
      long totalDuration = mediaPlayer.getDuration(); 
      long currentDuration = mediaPlayer.getCurrentPosition(); 

      seekBar.setProgress(progress); 
      nowduration.setText("" + utils.milliSecondsToTimer(currentDuration)); 
      totalduration.setText(""+utils.milliSecondsToTimer(totalDuration)); 

      // Running this thread after 100 milliseconds 
      mHandler.postDelayed(this, 100); 
     } 
    }; 

的問題是,經過一段時間後什麼媒體播放服務(5- 10秒)活動停止,我在logcat中看到的唯一錯誤就是這個。雖然刪除nowduration.setText所有工作正常,seekbar正在更新沒有任何問題,爲什麼TextView活動停止?

04-10 10:22:40.610  288-813/system_process I/ActivityManager﹕ Process com.package.appname (pid 3734) has died. 
04-10 10:22:40.610  288-3633/system_process I/WindowManager﹕ WIN DEATH: Window{424b5708 com.package.appname/com.package.appname.MainActivity paused=false} 

我的嘗試:1.使用上TextView.setText runOnUithread - 同樣的行爲2.使用一個線程,而不是處理程序 - 但我有問題回採線程,有時我需要調用Handler.removeCallBacks,但我知道停止線程是一個非常糟糕的操作,這就是爲什麼Thread.stop(已折舊)。

+0

你使用線程以外的處理程序?你可以使用'Handler.removeCallBacks'。我認爲你誤解了處理程序的使用http://developer.android.com/reference/android/os/Handler.html併發布完整的棧跟蹤 – Raghunandan

回答

1

的所述用戶接口只能由UI線程進行更新。你需要一個處理程序,張貼到UI線程:

private void startTimerThread() { 
Handler handler = new Handler(); 
Runnable runnable = new Runnable() { 
    private long startTime = System.currentTimeMillis(); 
    public void run() { 

      try { 
       Thread.sleep(1000); 
      }  
      catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      handler.post(new Runnable(){ 
       public void run() { 
     long totalDuration = mediaPlayer.getDuration(); 
     long currentDuration = mediaPlayer.getCurrentPosition(); 

     seekBar.setProgress(progress); 
     nowduration.setText("" + utils.milliSecondsToTimer(currentDuration)); 
     totalduration.setText(""+utils.milliSecondsToTimer(totalDuration)); 


      } 
     }); 
     } 

}; 
new Thread(runnable).start(); 
} 
+1

我沒有看到op的代碼中的後臺線程。 Op正在使用Handler。 'mHandler.postDelayed(this,100);'。如果你在ui線程中創建一個處理程序,它與之相關聯。引用**當您創建一個新的處理程序時,它將綁定到正在創建它的線程的線程/消息隊列 - 從這一點開始,它將向該消息隊列傳遞消息和可運行對象,並在它們出來時執行它們的消息隊列** – Raghunandan

+0

嗯......它看起來像Android操作系統錯誤...也許這篇文章可以幫助你http://stackoverflow.com/a/17955940/1145140 –

0

嘗試使用處理器的消息來做到這一點:

private Handler handler = new Handler(){ 

    @Override 
    public void handleMessage(Message msg) { 
     super.handleMessage(msg); 

     switch(msg.what){ 
      case 1:{ 
       long totalDuration = mediaPlayer.getDuration(); 
       long currentDuration = mediaPlayer.getCurrentPosition(); 

       seekBar.setProgress(progress); 
       nowduration.setText("" + utils.milliSecondsToTimer(currentDuration)); 
       totalduration.setText(""+utils.milliSecondsToTimer(totalDuration)); 

       // Running this thread after 100 milliseconds 
       mHandler.sendEmptyMessageDelayed(1, 100); 
      }break; 
     } 
    } 

}; 

而且第一個更新的開始:

... 
handler.sendEmptyMessage(1); 
... 
+0

我不知道爲什麼,但這給我和我的代碼一樣的行爲。活動關閉。我認爲這是內存泄漏,但不知道爲什麼。也許這與我的手機有些麻煩? – artouiros

+0

您的播放器在錯誤原因之前是否完成播放? –

+0

不,活動停止,媒體播放器服務也停止。問題出在textView中,因爲在刪除TextView.setText()時,你的代碼工作得很好。 – artouiros