我正在尝试停止并释放onDestroy()中的MediaPlayer对象,但该应用程序一直在崩溃。这是代码:
void StartSound(final String ReaderPath) {
mediaplayer = MediaPlayer.create(test2.this, Uri.parse(ReaderPath));
try {
mediaplayer.start();
mediaplayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
current++;
try {
mp.reset();
mp.setDataSource(ReaderPath);
mp.prepare();
} catch (Exception e) {
e.printStackTrace();
}
mp.start();
}
});
} catch (Exception e) {
}
}
@Override
protected void onPause() {
if (mediaplayer != null && mediaplayer.isPlaying())
mediaplayer.pause();
super.onPause();
}
@Override
protected void onStop() {
if (mediaplayer != null && mediaplayer.isPlaying()) {
mediaplayer.release();
mediaplayer.stop();
}
super.onDestroy();
}
logcat的:
08-21 22:52:26.512: E/AndroidRuntime(16410): java.lang.RuntimeException: Unable to destroy activity {com.example.test/com.example.test.test2}: java.lang.NullPointerException
08-21 22:52:26.512: E/AndroidRuntime(16410): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3235)
08-21 22:52:26.512: E/AndroidRuntime(16410): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3253)
08-21 22:52:26.512: E/AndroidRuntime(16410): at android.app.ActivityThread.access$1200(ActivityThread.java:132)
08-21 22:52:26.512: E/AndroidRuntime(16410): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1190)
08-21 22:52:26.512: E/AndroidRuntime(16410): at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 22:52:26.512: E/AndroidRuntime(16410): at android.os.Looper.loop(Looper.java:137)
08-21 22:52:26.512: E/AndroidRuntime(16410): at android.app.ActivityThread.main(ActivityThread.java:4575)
08-21 22:52:26.512: E/AndroidRuntime(16410): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 22:52:26.512: E/AndroidRuntime(16410): at java.lang.reflect.Method.invoke(Method.java:511)
08-21 22:52:26.512: E/AndroidRuntime(16410): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-21 22:52:26.512: E/AndroidRuntime(16410): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-21 22:52:26.512: E/AndroidRuntime(16410): at dalvik.system.NativeStart.main(Native Method)
08-21 22:52:26.512: E/AndroidRuntime(16410): Caused by: java.lang.NullPointerException
08-21 22:52:26.512: E/AndroidRuntime(16410): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1406)
08-21 22:52:26.512: E/AndroidRuntime(16410): at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1876)
08-21 22:52:26.512: E/AndroidRuntime(16410): at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:316)
08-21 22:52:26.512: E/AndroidRuntime(16410): at android.app.Activity.performDestroy(Activity.java:4629)
08-21 22:52:26.512: E/AndroidRuntime(16410): at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1079)
08-21 22:52:26.512: E/AndroidRuntime(16410): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3222)
08-21 22:52:26.512: E/AndroidRuntime(16410): ... 11 more
为什么会这样?
答案 0 :(得分:3)
为什么要从onStop()方法调用super.onDestroy()?你应该调用super.onStop()。或者这是打字错误?
答案可能是您在调用stop()
之后在MediaPlayer
上致电release()
。致电release()
后MediaPlayer
无效且无法再使用。
答案 1 :(得分:1)
在MediaPlayer
方法中停止和释放onDestroy()
控件的正确方法是:
@Override
protected void onDestroy() {
if (mediaplayer != null) {
mediaplayer.stop();
mediaplayer = null;
}
super.onDestroy();
}