问题描述
我有两个按钮,它会播放声音,通知有关正确的选择,还是错的。这就是我如何做到这一点:
MediaPlayer的playError = MediaPlayer.create(QuizActivity.this,R.raw.error);
playError.start();
同样的,正确的声音。它工作正常的大部分时间,但是当我点击了很多次,在随机时间,我得到这个错误:
基本上它说行 playError.start(); 给我的 NullPointerException异常(只是有时)
07-21 23:05:32.767:ERROR / PlayerDriver(1287):命令PLAYER_ prePARE完成,错误或信息PVMFErrResource,-17
07-21 23:05:32.767:ERROR / MediaPlayer的(14449):错误(1,-17)
07-21 23:05:32.767:ERROR / MediaPlayer的(14449):MEDIA_ERROR(M prepareSync)信号的应用程序线程
07-21 23:05:32.777:ERROR / AndroidRuntime(14449):致命异常:主要
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(本机方法)
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(本机方法)
编辑:
线是148:playError.start();
和falseAnswerPoints()是:
公共无效falseAnswerPoints(){
MediaPlayer的playError = MediaPlayer.create(QuizActivity.this,R.raw.error);
playError.start();
}
同样是correctAnswerPoints,但有不同的声音。这就是它。有时,它给的NullPointerException一个,有时又...
答:
MediaPlayer的playSuccess = MediaPlayer.create(QuizActivity.this,R.raw.success);
playSuccess.start();
playSuccess.setOnCompletionListener(新OnCompletionListener(){
@覆盖
公共无效onCompletion(MediaPlayer的playSuccess){
playSuccess.release();
}
});
看来的MediaPlayer
不能创建,在创建()
方法返回一个空
指针。该官方文档说,出现这种情况的时候创建失败,没有进一步的细节。
您说出现这种情况,当你点击几次一排,导致这个方法被调用的按钮。这很可能是由于非重入的问题。
您应该尝试一个标志,该标志将prevent重入包围了的MediaPlayer
创建和使用:
公共无效falseAnswerPoints(){
如果(!mPlayingSound){
mPlayingSound = TRUE;
MediaPlayer的playError = MediaPlayer.create(QuizActivity.this,R.raw.error);
playError.start();
}
}
mPlayingSound
是一个私人布尔
成员初始化为假
和,你会恢复到假
一旦的MediaPlayer
将播放完毕(使用公开无效setOnCompletionListener(MediaPlayer.OnCompletionListener监听器)
应罚款,但我不知道是否这就是所谓的非正常情况或预期终止比赛)的。
修改:有一个NullPointerException异常所以有一个堆栈跟踪。要捕获堆栈跟踪,调试只(低于code是不适合放),你可以做如下:
公共无效falseAnswerPoints(){
尝试 {
MediaPlayer的playError = MediaPlayer.create(QuizActivity.this,R.raw.error);
playError.start();
}
赶上(NullPointerException异常E){
//设置断点那里检查你的应用程序的状态
//然后重新抛出异常把它记录下来,何乐不为
//记录额外的信息。
}
}
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();
}
});
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.
}
}
这篇关于NullPointerException异常而采用Android的媒体播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!