0

我在使用 Android 媒体播放器时遇到问题,我正在做一些小测验,并且我有方法 correctAnswer(),我在其中创建带有一些声音的 MediaPlayer,然后准备它,然后启动它,但是我得到了这个 NullPointerException 错误。

我正在尝试播放 mp3 声音。

这是该方法正确答案():

    private void correctAnswer() {
    MediaPlayer correctSound = MediaPlayer.create(this, R.raw.correct);
    try {
        correctSound.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    correctSound.start();

    Toast.makeText(getApplicationContext(), "Correct answer +1",
            Toast.LENGTH_SHORT).show();
    progressBar.incrementProgressBy(1);
    tvProgress.setText(progressBar.getProgress() + "/20");
    generateQuestion();
    endGame();
}

这是LogCat:

09-04 21:21:30.546: E/AndroidRuntime(513): Uncaught handler: thread main exiting due to         uncaught exception
09-04 21:21:30.567: E/AndroidRuntime(513): java.lang.NullPointerException
09-04 21:21:30.567: E/AndroidRuntime(513):  at   com.example.americansportsquiz.PlayStageActivity.correctAnswer(PlayStageActivity.java:121)
09-04 21:21:30.567: E/AndroidRuntime(513):  at com.example.americansportsquiz.PlayStageActivity.onClick(PlayStageActivity.java:75)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.View.performClick(View.java:2364)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.View.onTouchEvent(View.java:4179)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.widget.TextView.onTouchEvent(TextView.java:6541)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.View.dispatchTouchEvent(View.java:3709)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-04 21:21:30.567: E/AndroidRuntime(513):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
09-04 21:21:30.567: E/AndroidRuntime(513):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
09-04 21:21:30.567: E/AndroidRuntime(513):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.os.Looper.loop(Looper.java:123)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.app.ActivityThread.main(ActivityThread.java:4363)
09-04 21:21:30.567: E/AndroidRuntime(513):  at java.lang.reflect.Method.invokeNative(Native Method)
09-04 21:21:30.567: E/AndroidRuntime(513):  at java.lang.reflect.Method.invoke(Method.java:521)
09-04 21:21:30.567: E/AndroidRuntime(513):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-04 21:21:30.567: E/AndroidRuntime(513):  at    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-04 21:21:30.567: E/AndroidRuntime(513):  at dalvik.system.NativeStart.main(Native Method)

编辑正如我所说,这是测验应用程序,它会为 7 个正确答案播放声音,但它在 8 个时失败,并给我这个空指针异常。在它以 8 个正确答案失败之前,它一直给我这个警告。

09-04 21:41:33.086: W/MediaPlayer(598): info/warning (1, 44)
4

2 回答 2

1

这个人也有类似的问题:Android SDK Mediaplayer.create random returns null

也许您应该尝试使用 SoundPool,因为您正在播放的音频文件只是一个短片。此外,如果您使用 SoundPool,您可以加载R.raw.correct一次而不是每次加载。

无论如何,SoundPool 通常更适合此类短音。=]

这是一个关于如何开始使用 SoundPool 的好教程:http ://www.vogella.com/articles/AndroidMedia/article.html#tutorial_soundpool

于 2012-09-04T21:45:46.083 回答
1

在您尝试创建新的 MediaPlayer 之前,您似乎没有发布以前的 MediaPlayer。只需保留对此 MediaPlayer 的引用并重放声音,而不是每次都创建一个新副本:

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();
    ...
}
于 2012-09-04T21:46:01.537 回答