0

我正在使用媒体播放器播放 mp3 文件。但是当我运行它时,我得到空指针异常。请告诉我是什么问题。

这是代码:

package com.example.soundplayer;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    /**
     * Variables
     */
    MediaPlayer mp = null;
    String hello = "Hello!";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /**
         * Talking with the buttonHello
         */
        final Button buttonHello = (Button) findViewById(R.id.idHello);
        buttonHello.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                managerOfSound(hello);
            } // END onClick()
        }); // END buttonHello
    } // END onCreate()

    /**
     * Manager of Sounds
     */
    protected void managerOfSound(String theText) {
        if (mp != null) {
            mp.reset();
            mp.release();
        }
        if (theText == hello){
            mp = new MediaPlayer();
            mp = MediaPlayer.create(MainActivity.this, R.raw.sound);
            mp.start();}
    }
}

这是一个例外:

08-22 13:13:01.001: W/dalvikvm(785): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
08-22 13:13:01.001: E/AndroidRuntime(785): Uncaught handler: thread main exiting due to uncaught exception
08-22 13:13:01.020: E/AndroidRuntime(785): java.lang.NullPointerException
08-22 13:13:01.020: E/AndroidRuntime(785):  at com.example.soundplayer.MainActivity.managerOfSound(MainActivity.java:57)
08-22 13:13:01.020: E/AndroidRuntime(785):  at com.example.soundplayer.MainActivity$1.onClick(MainActivity.java:30)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.view.View.performClick(View.java:2364)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.view.View.onTouchEvent(View.java:4179)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.widget.TextView.onTouchEvent(TextView.java:6541)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.view.View.dispatchTouchEvent(View.java:3709)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-22 13:13:01.020: E/AndroidRuntime(785):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
08-22 13:13:01.020: E/AndroidRuntime(785):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
08-22 13:13:01.020: E/AndroidRuntime(785):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.os.Looper.loop(Looper.java:123)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.app.ActivityThread.main(ActivityThread.java:4363)
08-22 13:13:01.020: E/AndroidRuntime(785):  at java.lang.reflect.Method.invokeNative(Native Method)
08-22 13:13:01.020: E/AndroidRuntime(785):  at java.lang.reflect.Method.invoke(Method.java:521)
08-22 13:13:01.020: E/AndroidRuntime(785):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-22 13:13:01.020: E/AndroidRuntime(785):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-22 13:13:01.020: E/AndroidRuntime(785):  at dalvik.system.NativeStart.main(Native Method)

我在这一行遇到异常:

 mp.start();

提前致谢。

4

4 回答 4

2

您必须从 create 方法获取结果并在使用该 mediaPlayer 之前检查该结果,因为如果创建成功,create 函数将返回媒体播放器对象,如果创建媒体播放器失败,则返回 NULL。

检查官方网站中该功能的描述

a MediaPlayer object, or null if creation failed

还有一件事你不需要调用 prepare() 函数,因为 On successfully created MediaPlayer , prepare() will already have been called and must not be called again.

您可以在调用 mp.start 之前检查 mp 是否为空

if(mp!=null)
  mp.start();
于 2012-08-22T08:04:28.883 回答
1

根据 MediaPlayer 状态图,必须先mp.prepare()调用mp.start().

在此处输入图像描述

于 2012-08-22T07:58:11.277 回答
0

尝试;

mp = MediaPlayer.create(MainActivity.this, R.raw.sound);
if(mp!=null) {
   mp.start();
}
于 2012-08-22T07:59:51.150 回答
0

这可能不是问题,但你应该改变它:

if (theText.equals(" hello"){
     mp = MediaPlayer.create(MainActivity.this, R.raw.sound);
    mp.prepare();
    mp.start();}
于 2012-08-22T08:02:42.753 回答