java - NullPointerException while using Android's mediaplayer -
i have 2 button , play sound notify right choice, or wrong one. how it:
mediaplayer playerror = mediaplayer.create(quizactivity.this, r.raw.error); playerror.start();
same correct sound. works fine of time, when click many times, @ random times error:
basically says line playerror.start(); gives me nullpointerexception (only sometimes)
07-21 23:05:32.767: error/playerdriver(1287): command player_prepare completed 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): @ com.quiz.quizactivity.falseanswerpoints(quizactivity.java:148) 07-21 23:05:32.777: error/androidruntime(14449): @ com.quiz.quizactivity$5.onclick(quizactivity.java:86) 07-21 23:05:32.777: error/androidruntime(14449): @ android.view.view.performclick(view.java:2408) 07-21 23:05:32.777: error/androidruntime(14449): @ android.view.view$performclick.run(view.java:8816) 07-21 23:05:32.777: error/androidruntime(14449): @ android.os.handler.handlecallback(handler.java:587) 07-21 23:05:32.777: error/androidruntime(14449): @ android.os.handler.dispatchmessage(handler.java:92) 07-21 23:05:32.777: error/androidruntime(14449): @ android.os.looper.loop(looper.java:123) 07-21 23:05:32.777: error/androidruntime(14449): @ android.app.activitythread.main(activitythread.java:4627) 07-21 23:05:32.777: error/androidruntime(14449): @ java.lang.reflect.method.invokenative(native method) 07-21 23:05:32.777: error/androidruntime(14449): @ java.lang.reflect.method.invoke(method.java:521) 07-21 23:05:32.777: error/androidruntime(14449): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) 07-21 23:05:32.777: error/androidruntime(14449): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) 07-21 23:05:32.777: error/androidruntime(14449): @ dalvik.system.nativestart.main(native method)
edit:
line 148: playerror.start();
and falseanswerpoints() is:
public void falseanswerpoints() { mediaplayer playerror = mediaplayer.create(quizactivity.this, r.raw.error); playerror.start(); }
same correctanswerpoints, different sound. that's it. gives nullpointerexception one, 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(); } });
it seems mediaplayer
cannot created, create()
method returning null
pointer. official doc says happens when creation fails, no further details.
you said happens when click several times in row on button leads method called. due non reentrancy issue.
you should try surround mediaplayer
creation , usage flag prevent reentrancy:
public void falseanswerpoints() { if (!mplayingsound) { mplayingsound = true; mediaplayer playerror = mediaplayer.create(quizactivity.this, r.raw.error); playerror.start(); } }
mplayingsound
being private boolean
member initialized false
, reset false
once mediaplayer
have finished playing (using public void setoncompletionlistener (mediaplayer.oncompletionlistener listener)
should fine, although not sure whether called in case of abnormal or anticipated termination of play).
edit: there nullpointerexception there stack trace. capture stack trace, in debug (the code below not suitable release), can follows:
public void falseanswerpoints() { try { mediaplayer playerror = mediaplayer.create(quizactivity.this, r.raw.error); playerror.start(); } catch (nullpointerexception e) { // set breakpoint there inspect state of app // rethrow exception have logged, , why not // log info. } }
Comments
Post a Comment