这是我在播放音频文件时遇到的异常问题。在我的混乱的文字游戏应用程序中,我有两个声音文件,即。 clap.mp3和oops.mp3。当用户分别提供正确答案和错误答案时,将播放这些文件。当用户提供正确答案时,播放音频文件clap.mp3并向用户提供下一个谜题。现在当用户提供错误的答案时,它应该播放oops.mp3。但我的应用程序,给出了一个ANR消息,要求我“强制关闭”它。请帮帮我。
代码:
public void checkWord()
{
if(abcd.equalsIgnoreCase(etGuessedWord.getText().toString()))
{
WordLibrary.setMyInt(WordLibrary.getMyInt() + 10);
tvScore.setText(String.valueOf(WordLibrary.getMyInt()));
mp = MediaPlayer.create(this, R.raw.clap);
mp.start();
new AlertDialog.Builder(JumbledWords.this).setMessage("Awesome!!!")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
wordIdx = (wordIdx + 1) % getSize();
sWord = getScrambledWord(word_list[wordIdx]);
tvScrambledWord.setText(sWord);
abcd = word_list[wordIdx];
++word_array_length;
mp.stop();
etGuessedWord.setText("");
if(word_array_length >= word_list.length)
{
new AlertDialog.Builder(JumbledWords.this).setMessage("Level Complete!!! ")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
start = 0;
finish();
}
}).create().show();
}
}
}).create().show();
}
else
{
mp = MediaPlayer.create(this, R.raw.oop);
mp.start();
new AlertDialog.Builder(JumbledWords.this).setMessage("Wrong. Try Again")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
mp.stop();
etGuessedWord.setText("");
}
}).create().show();
}
}
etGuessedWord.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
if(etGuessedWord.getText().toString().length() == abcd.length() )
{
checkWord();
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
});
异常消息
java.lang.NullPointerException
at com.myambitionconsultants.JumbledWords.checkWord(JumbledWords.java:447)
at com.myambitionconsultants.JumbledWords$1.afterTextChanged(JumbledWords.java:125)
at android.widget.TextView.sendAfterTextChanged(TextView.java:6281)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:6504)
at android.text.SpannableStringBuilder.sendTextHasChanged(SpannableStringBuilder.java:897)
at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:353)
at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:583)
at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:174)
at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:120)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:247)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:73)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
Hiiii, Yor正在获取空指针异常意味着您的MediaPlayer可能未初始化。 所以你必须先进行初始化。你初始化了吗? 比如,mp = new MediaPlayer();之后你可以使用
答案 1 :(得分:0)
mp = MediaPlayer.create(this, R.raw.clap);
如果媒体播放器对象创建失败,则返回null。您应该尝试使用不同的数据源,如oops.mp3。这也可能是device related问题。