创建MediaPlayer时出错

时间:2015-07-22 21:35:37

标签: java android mp3

使用此错误日志创建MediaPlayer时,我的应用程序崩溃了:

07-22 23:20:15.279  29029-29029/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.linkpulsion.bibix, PID: 29029
        java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.linkpulsion.bibix/com.linkpulsion.bibix.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879)
                at android.app.ActivityThread.access$900(ActivityThread.java:182)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:6141)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
                at android.content.ContextWrapper.getResources(ContextWrapper.java:90)
                at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
                at android.media.MediaPlayer.create(MediaPlayer.java:986)
                at android.media.MediaPlayer.create(MediaPlayer.java:969)
                at com.linkpulsion.bibix.MainActivity.<init>(MainActivity.java:34)
                at java.lang.reflect.Constructor.newInstance(Native Method)
                at java.lang.Class.newInstance(Class.java:1650)
                at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879)
                at android.app.ActivityThread.access$900(ActivityThread.java:182)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:6141)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

第34行指向我创建的第一个MediaPlayer。 这是我的主要课程:

    private final MediaPlayer arracherlagorge = MediaPlayer.create(this,R.raw.arracherlagorge);

    Button arracherlagorgeBut = (Button)findViewById(R.id.arracherLaGorgeBut);

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

        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);

        arracherlagorgeBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (arracherlagorge.isPlaying() || arracherlagorge.isLooping()) {
                    try {
                        arracherlagorge.pause();
                        arracherlagorge.seekTo(0);
                        arracherlagorge.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    if (arracherlagorge == null) {
                        Log.d("MEDIAPLAYER", "Error while creating MediaPlayer");
                    } else {
                        arracherlagorge.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                            @Override
                            public void onCompletion(MediaPlayer mp) {
                                arracherlagorge.stop();
                                arracherlagorge.release();
                            }
                        });
                        arracherlagorge.start();
                    }
                }
            }
        });

我的MP3文件比特率和格式符合128kb / s和44.1 kHz等规格:http://developer.android.com/guide/appendix/media-formats.html 我正在为API 16+工作并在API 21上进行测试。

1 个答案:

答案 0 :(得分:0)

而不是

    private final MediaPlayer arracherlagorge = MediaPlayer.create(this,R.raw.arracherlagorge);

试试这个:

    private final MediaPlayer arracherlagorge = MediaPlayer.create(getResources(), R.raw.arracherlagorge);