简体   繁体   中英

Android activity crashes when resuming

My app has a button to open a webpage, however when returning to the app it crashes, I am using onPause() and onRestart() methods. Anyone know why it crashes when resuming to the app after closing the webpage?

private void goToUrl(String url) {
    Uri uriUrl = Uri.parse(url);

    Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);

    startActivity(launchBrowser);
}

@Override
protected void onPause() {
    super.onPause();

    titleMusic.release();
}

@Override
protected void onRestart() {
    super.onRestart();

    titleMusic.start();
}

logCat stack trace

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity java.lang.IllegalStateException
04-23 15:49:09.886: E/AndroidRuntime(18310):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2836)
04-23 15:49:09.886: E/AndroidRuntime(18310):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2865)
04-23 15:49:09.886: E/AndroidRuntime(18310):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1288)
04-23 15:49:09.886: E/AndroidRuntime(18310):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-23 15:49:09.886: E/AndroidRuntime(18310):    at android.os.Looper.loop(Looper.java:212)
04-23 15:49:09.886: E/AndroidRuntime(18310):    at android.app.ActivityThread.main(ActivityThread.java:5135)
04-23 15:49:09.886: E/AndroidRuntime(18310):    at java.lang.reflect.Method.invokeNative(Native Method)
04-23 15:49:09.886: E/AndroidRuntime(18310):    at java.lang.reflect.Method.invoke(Method.java:515)
04-23 15:49:09.886: E/AndroidRuntime(18310):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
04-23 15:49:09.886: E/AndroidRuntime(18310):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
04-23 15:49:09.886: E/AndroidRuntime(18310):    at dalvik.system.NativeStart.main(Native Method)
04-23 15:49:09.886: E/AndroidRuntime(18310): Caused by: java.lang.IllegalStateException
04-23 15:49:09.886: E/AndroidRuntime(18310):    at android.media.MediaPlayer._start(Native Method)
04-23 15:49:09.886: E/AndroidRuntime(18310):    at android.media.MediaPlayer.start(MediaPlayer.java:1097)
04-23 15:49:09.886: E/AndroidRuntime(18310):    at com.example.hunglikeanandroid.TitleScreen.onRestart(TitleScreen.java:364)
04-23 15:49:09.886: E/AndroidRuntime(18310):    at android.app.Instrumentation.callActivityOnRestart(Instrumentation.java:1181)
04-23 15:49:09.886: E/AndroidRuntime(18310):    at android.app.Activity.performRestart(Activity.java:5291)
04-23 15:49:09.886: E/AndroidRuntime(18310):    at android.app.Activity.performResume(Activity.java:5302)
04-23 15:49:09.886: E/AndroidRuntime(18310):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2826)
04-23 15:49:09.886: E/AndroidRuntime(18310): Caused by: java.lang.IllegalStateException 
04-23 15:49:09.886: E/AndroidRuntime(18310): at android.media.MediaPlayer._start(Native Method)

This exception indicates that your media player is in a "wrong" state. Being a state machine, it must be properly initialised , make sure that you properly deal with it's state when going out/coming back to the activity.

For example, in your onPause(), you're releasing MediaPlayer resources, so when you're back, it's not initialised again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM