Pol Hallen
Pol Hallen

Reputation: 1862

mediaplayer and dialog

I've an "info" dialog and I'd like add background music. I added my music in res/raw, just open dialog there's a check by sharedpreferences (a boolean to check if audio already started) and 2 methods: musicStart() and musicStop()

so, before onCreate I added:

MediaPlayer mp = MediaPlayer.create(this, R.raw.sob);

but app crash with:

12-06 18:54:14.425: E/AndroidRuntime(414): Caused by: java.lang.NullPointerException

I don't understand why

Upvotes: 1

Views: 294

Answers (1)

FoamyGuy
FoamyGuy

Reputation: 46856

You need to put that line inside of onCreate(), not before it. so it should be like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MediaPlayer mp = MediaPlayer.create(this, R.raw.sob);
    //...everything else you need to do
}

Upvotes: 2

Related Questions