简体   繁体   English

致命异常:主java.lang.IllegalStateException当按Back时

[英]FATAL EXCEPTION: main java.lang.IllegalStateException When press Back

I have Runnable that runs in Thread in a MediaPlayer. 我有在MediaPlayer中的Thread中运行的Runnable。 When i press back I get an IllegalStateException. 当我按回时,我得到一个IllegalStateException。

Here is the Runnable: 这是Runnable:

mp is MediaPlayer mp是MediaPlayer

private Runnable mUpdateTimeTask = new Runnable() {
       public void run() //This line is AndroidBuildingMusicPlayerActivity.java:318
       {

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

OnDetroy method: OnDetroy方法:

@Override
 public void onDestroy(){
 super.onDestroy();
    mp.release();
 }

Error log: 错误日志:

 FATAL EXCEPTION: main
java.lang.IllegalStateException
        at android.media.MediaPlayer.getDuration(Native Method)
        at com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity$9.run(AndroidBuildingMusicPlayerActivity.java:318)
        at android.os.Handler.handleCallback(Handler.java:587)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:130)
        at android.app.ActivityThread.main(ActivityThread.java:3683)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)

I suspect that you called mp.pause() in OnPause() method of this activity. 我怀疑您在此活动的OnPause()方法中调用了mp.pause() So when you clicked back button, MediaPayer went into pause status. 因此,当您单击“后退”按钮时,MediaPayer进入了暂停状态。 However since your Runnable is called asynchronous, so it will call mp.getDuration() in an invalid state. 但是,由于您的Runnable称为异步,因此它将在无效状态下调用mp.getDuration()。 To fix this, please add in the check: 要解决此问题,请添加检查:

if (mp.isPlaying()) {
   long totalDuration = mp.getDuration();
   long currentDuration = mp.getCurrentPosition();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命异常(java.lang.IllegalStateException) - Fatal exception (java.lang.IllegalStateException) 致命异常:java.lang.IllegalStateException没有活动 - Fatal Exception: java.lang.IllegalStateException No activity “java.lang.IllegalStateException:无法对密封实例执行此操作。” 致命例外:主要 - "java.lang.IllegalStateException: Cannot perform this action on a sealed instance." FATAL EXCEPTION: main 致命异常:java.lang.IllegalStateException尚未连接GoogleApiClient - Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet 使用Selenium的线程“main”java.lang.IllegalStateException中的异常 - Exception in thread “main” java.lang.IllegalStateException for using Selenium 线程“main”中的异常 java.lang.IllegalStateException:已连接 - Exception in thread “main” java.lang.IllegalStateException: Already connected 线程“main”中的异常 java.lang.IllegalStateException:驱动程序可执行文件是一个目录 - Exception in thread “main” java.lang.IllegalStateException: The driver executable is a directory Android java.lang.IllegalStateException,不在主线程上 - Android java.lang.IllegalStateException, not on the main thread java.lang.IllegalStateException:不在主线程上 - java.lang.IllegalStateException: Not on the main thread java.lang.IllegalStateException:isHexDigit异常 - java.lang.IllegalStateException: isHexDigit exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM