我的应用程序有一个打开网页的按钮,但是当它返回到应用程序时它会崩溃,我正在使用onPause()和onRestart()方法。任何人都知道在关闭网页后恢复到应用程序时崩溃的原因吗?
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堆栈跟踪
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)
答案 0 :(得分:1)
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)
此异常表示您的媒体播放器处于“错误”状态。作为状态机,它必须正确initialised,确保在外出/回到活动时正确处理它的状态。
例如,在你的onPause()中,你正在释放MediaPlayer资源,所以当你回来时,它不会再次初始化。