-1

I am trying to stop a melody from playing when user hit the back-button. This code is working when the file is playing, if not there will be an error and the app shuts down. How come it isnt working?

MediaPlayer melodisnutt;
public void onStop() {
    super.onStop();
    if ( melodisnutt.isPlaying () ) {
        melodisnutt.stop();
    }
}
4

2 回答 2

1

如果您确定错误发生在 onStop() 方法中,则它看起来像一个 nullPointerException。

尝试更换:

if(melodisnutt.isPlaying()){

经过

if(melodisnutt != null && melodisnutt.isPlaying()){
于 2012-06-07T09:23:48.563 回答
0

最好检查您的 MediaPlayer 是否不为空,

public void onStop() {
    super.onStop();
    if (melodisnutt.isPlaying () && melodisnutt != null) {
        melodisnutt.stop();
    }
}
于 2012-06-07T09:22:46.323 回答