I have a problem with Android media player, I’m working on some little quiz, and I have method correctAnswer(), where I create MediaPlayer with some sound, then prepare it, and then start it, but I get this NullPointerException error.
I am trying to play mp3 sound.
Here is that method correctAnswer():
private void correctAnswer() {
MediaPlayer correctSound = MediaPlayer.create(this, R.raw.correct);
try {
correctSound.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
correctSound.start();
Toast.makeText(getApplicationContext(), "Correct answer +1",
Toast.LENGTH_SHORT).show();
progressBar.incrementProgressBy(1);
tvProgress.setText(progressBar.getProgress() + "/20");
generateQuestion();
endGame();
}
Here is LogCat:
09-04 21:21:30.546: E/AndroidRuntime(513): Uncaught handler: thread main exiting due to uncaught exception
09-04 21:21:30.567: E/AndroidRuntime(513): java.lang.NullPointerException
09-04 21:21:30.567: E/AndroidRuntime(513): at com.example.americansportsquiz.PlayStageActivity.correctAnswer(PlayStageActivity.java:121)
09-04 21:21:30.567: E/AndroidRuntime(513): at com.example.americansportsquiz.PlayStageActivity.onClick(PlayStageActivity.java:75)
09-04 21:21:30.567: E/AndroidRuntime(513): at android.view.View.performClick(View.java:2364)
09-04 21:21:30.567: E/AndroidRuntime(513): at android.view.View.onTouchEvent(View.java:4179)
09-04 21:21:30.567: E/AndroidRuntime(513): at android.widget.TextView.onTouchEvent(TextView.java:6541)
09-04 21:21:30.567: E/AndroidRuntime(513): at android.view.View.dispatchTouchEvent(View.java:3709)
09-04 21:21:30.567: E/AndroidRuntime(513): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-04 21:21:30.567: E/AndroidRuntime(513): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-04 21:21:30.567: E/AndroidRuntime(513): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-04 21:21:30.567: E/AndroidRuntime(513): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-04 21:21:30.567: E/AndroidRuntime(513): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-04 21:21:30.567: E/AndroidRuntime(513): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-04 21:21:30.567: E/AndroidRuntime(513): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
09-04 21:21:30.567: E/AndroidRuntime(513): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
09-04 21:21:30.567: E/AndroidRuntime(513): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
09-04 21:21:30.567: E/AndroidRuntime(513): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
09-04 21:21:30.567: E/AndroidRuntime(513): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
09-04 21:21:30.567: E/AndroidRuntime(513): at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 21:21:30.567: E/AndroidRuntime(513): at android.os.Looper.loop(Looper.java:123)
09-04 21:21:30.567: E/AndroidRuntime(513): at android.app.ActivityThread.main(ActivityThread.java:4363)
09-04 21:21:30.567: E/AndroidRuntime(513): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 21:21:30.567: E/AndroidRuntime(513): at java.lang.reflect.Method.invoke(Method.java:521)
09-04 21:21:30.567: E/AndroidRuntime(513): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-04 21:21:30.567: E/AndroidRuntime(513): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-04 21:21:30.567: E/AndroidRuntime(513): at dalvik.system.NativeStart.main(Native Method)
EDIT As I said, this is quiz app, and it play sound for 7 correct answers, but it fails on 8, and give me this null pointer expcetion. Before it fails at 8 correct answer it give me all time this warning.
09-04 21:41:33.086: W/MediaPlayer(598): info/warning (1, 44)
It looks like your are not releasing the previous MediaPlayer before you are trying to create a new one. Simply keep a reference to this MediaPlayer and replay the sound, rather than creating a new copy every time: