0

I am a noob programer and I encountered a problem, I have a mediaplayer.start() inside an onClick method, if i call

mediaplayer.stop(); 
mediaplayer.reset();
mediaplayer.release();

it will crash and get the error:

Attempt to invoke virtual method 'void android.media.MediaPlayer.stop()' on a null object reference ".

I tried How to stop the mediaplayer playing from other methods but when I used it I've got a:

"Unable to destroy activity {...Activity}: java.lang.IllegalStateException"

Any support is highly appreciated

Community
  • 1
  • 1

1 Answers1

0

Just check if the MediaPlayer is null:

if(mediaplayer != null) {
    mediaplayer.stop(); 
    mediaplayer.reset();
    mediaplayer.release();
}
ByteHamster
  • 4,884
  • 9
  • 38
  • 53