开发者

Why the MediaPlayer object is giving an exception?

This is my question about the exception which I am getting when I am playing an audio file. In my Jumbled words game app, I have two sound files, viz. clap.mp3 and oops.mp3. These files are played when开发者_运维百科 the user provides a correct answer and wrong answer respectively. When the user provides the correct answer, the audio file clap.mp3 gets played and the user is provided with the next puzzle. Now when the user provides wrong answer to this puzzle, it should play oops.mp3. But my app, gives a ANR message that ask me to "Force Close" it. Please help me.

Code:

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

        }

    });

Exception Message

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)


Hiiii, Yor are getting null pointer exception means your MediaPlayer may not initialized. so you have to initialize first. Are u initialize? like, mp = new MediaPlayer(); after that u can use


 mp = MediaPlayer.create(this, R.raw.clap);

Returns null if the media player object creation failed. You should try using a different datasource like oops.mp3. This also could be a device related problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜