I have two button and it will play a sound to notify about right choice, or wrong one.
This is how I do it:
MediaPlayer playError = MediaPlayer.create(QuizActivity.this, R.raw.error);
playError.start();
Same with correct sound. It works fine most of the time, but when I click it many times, at random times I get this error:
Basically it says line playError.start(); gives me NullPointerException (only sometimes)
07-21 23:05:32.767: ERROR/PlayerDriver(1287): Command PLAYER_PREPARE completed with an error or info PVMFErrResource, -17
07-21 23:05:32.767: ERROR/MediaPlayer(14449): error (1, -17)
07-21 23:05:32.767: ERROR/MediaPlayer(14449): MEDIA_ERROR(mPrepareSync) signal application thread
07-21 23:05:32.777: ERROR/AndroidRuntime(14449): FATAL EXCEPTION: main
07-21 23:05:32.777: ERROR/AndroidRuntime(14449): java.lang.NullPointerException
07-21 23:05:32.777: ERROR/AndroidRuntime(14449): at com.quiz.QuizActivity.falseAnswerPoints(QuizActivity.java:148)
07-21 23:05:32.777: ERROR/AndroidRuntime(14449): at com.quiz.QuizActivity$5.onClick(QuizActivity.java:86)
07-21 23:05:32.777: ERROR/AndroidRuntime(14449): at android.view.View.performClick(View.java:2408)
07-21 23:05:32.777: ERROR/AndroidRuntime(14449): at android.view.View$PerformClick.run(View.java:8816)
07-21 23:05:32.777: ERROR/AndroidRuntime(14449): at android.os.Handler.handleCallback(Handler.java:587)
07-21 23:05:32.777: ERROR/AndroidRuntime(14449): at android.os.Handler.dispatchMessage(Handler.java:92)
07-21 23:05:32.777: ERROR/AndroidRuntime(14449): at android.os.Looper.loop(Looper.java:123)
07-21 23:05:32.777: ERROR/AndroidRuntime(14449): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-21 23:05:32.777: ERROR/AndroidRuntime(14449): at java.lang.reflect.Method.invokeNative(Native Method)
07-21 23:05:32.777: ERROR/AndroidRuntime(14449): at java.lang.reflect.Method.invoke(Method.java:521)
07-21 23:05:32.777: ERROR/AndroidRuntime(14449): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-21 23:05:32.777: ERROR/AndroidRuntime(14449): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-21 23:05:32.777: ERROR/AndroidRuntime(14449): at dalvik.system.NativeStart.main(Native Method)
Edit:
Line is 148: playError.start();
And falseAnswerPoints() is:
public void falseAnswerPoints() {
MediaPlayer playError = MediaPlayer.create(QuizActivity.this, R.raw.error);
playError.start();
}
Same is correctAnswerPoints, but with different sound. That's about it. Sometimes it gives NullPointerException with one, sometimes with another...
Answer:
MediaPlayer playSuccess = MediaPlayer.create(QuizActivity.this, R.raw.success);
playSuccess.start();
playSuccess.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer playSuccess) {
playSuccess.release();
}
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…