Search code examples
androidandroid-mediaplayeronpause

Android onPause() causing Activity to crash


for some reason my Activity crash when I finish that specified activity. Before when I have only the onDestroy everything works fine except when i minimize the application (HOME button), the music still plays.

From what I gathered, I end up using onDestroy(), onResume() and onPause() to manipulate the MediaPlayer... but for some reason, when the activity finished, An Alert Dialog stating that Unforunately, ProgramX has stopped. ALTHOUGH, it continues on to the next startActivity(intent) like it should.

MediaPlayerAssets gls; // custom MediaPlayer Assets to fetch MediaPlayer objects
@Override 
protected void onPause()
{
    super.onPause();
    gls.glbgm.pause(); // my logCat demands that the error occurs here.
    resumeToPosition = gls.glbgm.getCurrentPosition();
}


@Override
protected void onRestart()
{
    super.onRestart();
    if(gls.glbgm != null && !gls.glbgm.isPlaying())
    {
        gls.glbgm.seekTo(resumeToPosition);
        gls.glbgm.start();
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    gls.glbgm.release();
    gls.score.release();
    gls.loser.release();
}

  • E/AndroidRuntime(23422): FATAL EXCEPTION: main
  • E/AndroidRuntime(23422): java.lang.RuntimeException: Unable to pause activity {com.fenrirAB.request/getluckyPkg.GetLuckyGame}: java.lang.IllegalStateException
  • E/AndroidRuntime(23422): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3112)
  • E/AndroidRuntime(23422): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3067)
  • E/AndroidRuntime(23422): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3045)
  • E/AndroidRuntime(23422): at android.app.ActivityThread.access$900(ActivityThread.java:152)
  • E/AndroidRuntime(23422): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1295)
  • E/AndroidRuntime(23422): at android.os.Handler.dispatchMessage(Handler.java:99)
  • E/AndroidRuntime(23422): at android.os.Looper.loop(Looper.java:137)
  • E/AndroidRuntime(23422): at android.app.ActivityThread.main(ActivityThread.java:5299)
  • E/AndroidRuntime(23422): at java.lang.reflect.Method.invokeNative(Native Method)
  • E/AndroidRuntime(23422): at java.lang.reflect.Method.invoke(Method.java:511)
  • E/AndroidRuntime(23422): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
  • E/AndroidRuntime(23422): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
  • E/AndroidRuntime(23422): at dalvik.system.NativeStart.main(Native Method)
  • E/AndroidRuntime(23422): Caused by: java.lang.IllegalStateException
  • E/AndroidRuntime(23422): at android.media.MediaPlayer._pause(Native Method)
  • E/AndroidRuntime(23422): at android.media.MediaPlayer.pause(MediaPlayer.java:1372)
  • E/AndroidRuntime(23422): at getluckyPkg.GetLuckyGame.onPause(GetLuckyGame.java:357)
  • E/AndroidRuntime(23422): at android.app.Activity.performPause(Activity.java:5447)
  • E/AndroidRuntime(23422): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1243)
  • E/AndroidRuntime(23422): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3098)
  • E/AndroidRuntime(23422): ... 12 more


  • Solution

  • Can't pause the MediaPlayer if it hasn't been started (state diagram), that's why you get a java.lang.IllegalStateException