2012-12-13 248 views
2

我的音樂播放器應用程序似乎有問題。當我退出時,它會彈出強制關閉警告。這是我認爲可能是問題的調試之後事業的部分代碼:退出時應用程序關閉Android

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

       // Displaying Total Duration time 
       songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration)); 
       // Displaying time completed playing 
       songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration)); 

       // Updating progress bar 
       int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration)); 
       //Log.d("Progress", ""+progress); 
       songProgressBar.setProgress(progress); 

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

而這裏的logcat的:

12-13 13:26:01.700: E/AndroidRuntime(31838): FATAL EXCEPTION: main 
12-13 13:26:01.700: E/AndroidRuntime(31838): java.lang.IllegalStateException 
12-13 13:26:01.700: E/AndroidRuntime(31838): at android.media.MediaPlayer.getDuration(Native Method) 
12-13 13:26:01.700: E/AndroidRuntime(31838): at com.example.musicshare.AndroidBuildingMusicPlayerActivity$1.run(AndroidBuildingMusicPlayerActivity.java:341) 
12-13 13:26:01.700: E/AndroidRuntime(31838): at android.os.Handler.handleCallback(Handler.java:587) 
12-13 13:26:01.700: E/AndroidRuntime(31838): at android.os.Handler.dispatchMessage(Handler.java:92) 
12-13 13:26:01.700: E/AndroidRuntime(31838): at android.os.Looper.loop(Looper.java:123) 
12-13 13:26:01.700: E/AndroidRuntime(31838): at android.app.ActivityThread.main(ActivityThread.java:3691) 
12-13 13:26:01.700: E/AndroidRuntime(31838): at java.lang.reflect.Method.invokeNative(Native Method) 
12-13 13:26:01.700: E/AndroidRuntime(31838): at java.lang.reflect.Method.invoke(Method.java:507) 
12-13 13:26:01.700: E/AndroidRuntime(31838): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 
12-13 13:26:01.700: E/AndroidRuntime(31838): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 
12-13 13:26:01.700: E/AndroidRuntime(31838): at dalvik.system.NativeStart.main(Native Method) 

問題是與mp.getDuration我的看法,但我不知道如何解決它。

回答

5

看起來即使退出後,您的可運行任務仍會在後面運行。所以我的想法是,你可能忘了打電話給removeCallbacks()

我的建議是添加下面一行在你onBackPressed()或在您退出該應用程序的地方,

mHandler.removeCallbacks(mUpdateTimeTask); 

請務必聲明全局您的Runnable mUpdateTimeTask以便它可以通過你的代碼。

+0

感謝它爲我工作 –

0

應該postRunnable之前刪除回調

mHandler.removeCallbacks(mUpdateTimeTask); 
mHandler.post(...); 
相關問題