Android - Play Sound on Button Click - Null Pointer Exception

Play Sound on Button Click in Android giving Null Pointer Exception

The error is occurring because the sound .wav is not supported by the device that I am running on the test app. It is running on a different device that supports .wav file format. I have converted the file type to mp3 and now it is working on both devices.

Thanks to everyone for your reply...

NullPointerException while using Android's mediaplayer

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.
}
}

Button with sound OnClick causing NullPointerException

Simply use

ImageButton one = (ImageButton) findViewByID(R.id.imageButton)

in your onCreate() method and check your layout file if that is proper and have a Button with id imageButton

null pointer exception in radio Button

I suspect that the problem is here:

int selectedID = group.getCheckedRadioButtonId();
RadioButton radioButton = findViewById(selectedID);
if (radioButton.getText() == null) {
// ...
}

It looks like you're trying to handle the case where no radio button has yet been selected, but not going about it the right way.

getCheckedRadioButtonId() returns -1 when nothing has been selected. So you could check that immediately:

int selectedID = group.getCheckedRadioButtonId();
if (selectedID == -1) {
// ...
}

Or, if you want to work with the view specifically, you could check if the view is null (not if its text is null):

int selectedID = group.getCheckedRadioButtonId();
RadioButton radioButton = findViewById(selectedID);
if (radioButton == null) {
// ...
}

Android Sound NullPointerException error

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:

MediaPlayer correctSound;

private void correctAnswer() {
if(correctSound == null)
correctSound = MediaPlayer.create(this, R.raw.correct);

if(correctSound == null) {
// Something failed... try again or break out of this method
}

try {
correctSound.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

correctSound.start();
...
}


Related Topics



Leave a reply



Submit