我试图在 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();
}
日志猫:
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
为什么是这样?