Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
111 views
in Technique[技术] by (71.8m points)

java - NullPointerException while using Android's mediaplayer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It seems the MediaPlayer cannot be created, the create() method returning a null pointer. The official doc says this happens when the creation fails, with no further details.

You said this happens when you click several times in a row on the button that leads this method to be called. This is probably due to a non reentrancy issue.

You should try to surround the MediaPlayer creation and usage by a flag that would prevent reentrancy:

public void falseAnswerPoints() {
    if (!mPlayingSound) {
        mPlayingSound = true;

        MediaPlayer playError = MediaPlayer.create(QuizActivity.this, R.raw.error);
        playError.start();
    }
}

mPlayingSound being a private boolean member initialized to false and that you would reset to false once the MediaPlayer would have finished playing (using public void setOnCompletionListener (MediaPlayer.OnCompletionListener listener) should be fine, although I am not sure whether it is called in case of abnormal or anticipated termination of the play).

EDIT: There is a NullPointerException so there is a stack trace. To capture the stack trace, in debug only (the code below is not suitable for release), you can do as follows:

public void falseAnswerPoints() {
    try {
        MediaPlayer playError = MediaPlayer.create(QuizActivity.this, R.raw.error);
        playError.start();
    }
    catch (NullPointerException e) {
        // Set a breakpoint there to inspect the state of your app
        // Then rethrow the exception to have it logged, and why not
        // log extra info.
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...