gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:09:49 26 4
gpt4 key购买 nike

好的,我在同一 Activity 中获得了两个音轨,因此我为它们使用了两个媒体播放器。我一切正常。唯一的问题是调用 onPause() 时,音频轨道会在 Activity 暂停时暂停。

如果正在播放音轨 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)

最佳答案

在使用前检查 mediaPlayer1 和 2 是否为 null

关于android - onPause() 在调用 mediaplayer.isPlaying() 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38533467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com