onPause()在调用mediaplayer.isPlaying()时崩溃

时间:2016-07-22 18:50:14

标签: android android-mediaplayer

好的,所以我在同一个活动中有两个音轨,因此他们正在使用两个媒体播放器。我把一切都搞好了。唯一的问题是在调用onPause()时,当活动暂停时音频轨道会暂停。

如果正在播放音轨1,则暂停应用程序崩溃,因为mediaPlayer2导致空指针异常,而正在播放音轨2时则相反。

这里是Activity.java的代码

@Override
    protected void onPause() {
        super.onPause();

        if (mediaPlayer1.isPlaying()) {
            mediaPlayer1.pause();
            pp1.setImageResource(R.drawable.play);
        }

        if (mediaPlayer2.isPlaying()) {
            mediaPlayer2.pause();
            pp2.setImageResource(R.drawable.play);
        }
    }

错误

错误1

java.lang.RuntimeException: Unable to pause activity {com.music.track/com.music.track.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.media.MediaPlayer.isPlaying()' on a null object reference
                                                                               at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3454)
                                                                               at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3408)
                                                                               at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3383)
                                                                               at android.app.ActivityThread.access$1100(ActivityThread.java:154)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:152)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5497)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.media.MediaPlayer.isPlaying()' on a null object reference
                                                                               at com.music.track.MainActivity.onPause(MainActivity.java:186)
                                                                               at android.app.Activity.performPause(Activity.java:6397)
                                                                               at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1380)
                                                                               at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3435)
                                                                               at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3408) 
                                                                               at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3383) 
                                                                               at android.app.ActivityThread.access$1100(ActivityThread.java:154) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:152) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5497)

错误2

java.lang.RuntimeException: Unable to pause activity {com.music.test/com.music.test.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.media.MediaPlayer.isPlaying()' on a null object reference
                                                                                   at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3454)
                                                                                   at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3408)
                                                                                   at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3383)
                                                                                   at android.app.ActivityThread.access$1100(ActivityThread.java:154)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:152)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5497)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.media.MediaPlayer.isPlaying()' on a null object reference
                                                                                   at com.music.test.MainActivity.onPause(MainActivity.java:181)
                                                                                   at android.app.Activity.performPause(Activity.java:6397)
                                                                                   at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1380)
                                                                                   at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3435)
                                                                                   at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3408) 
                                                                                   at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3383) 
                                                                                   at android.app.ActivityThread.access$1100(ActivityThread.java:154) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:152) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5497)

1 个答案:

答案 0 :(得分:1)

检查mediaPlayer1& 2在使用之前为null