Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
退出Android时应用程序强制关闭_Android_Duration_Forceclose_Android Music Player - Fatal编程技术网

退出Android时应用程序强制关闭

退出Android时应用程序强制关闭,android,duration,forceclose,android-music-player,Android,Duration,Forceclose,Android Music Player,我的音乐播放器应用程序似乎有问题。当我退出时,它会弹出强制关闭警告。这是我认为可能是调试后出现问题的代码部分: private Runnable mUpdateTimeTask = new Runnable() { public void run() { long totalDuration = mp.getDuration(); long currentDuration = mp.getCurrentPositi

我的音乐播放器应用程序似乎有问题。当我退出时,它会弹出强制关闭警告。这是我认为可能是调试后出现问题的代码部分:

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);
           }
        };
下面是日志:

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上,但我不知道如何修复它。

看起来,即使退出,您的可运行任务仍然在后面运行。因此,我的问题是,您可能忘记调用
removeCallbacks()

我的建议是在您的
onBackPressed()
中或退出应用程序的位置添加以下行

 mHandler.removeCallbacks(mUpdateTimeTask);

确保全局声明可运行的mUpdateTimeTask,以便它在整个代码中都可用

应在postRunnable之前删除回调

mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.post(...);