Reputation: 141
Okay so I got two audio tracks in the same activity and hence am using two media players for them. I've got everything working fine. The only problem is when calling the onPause() so that the audio track gets paused when activity pauses.
If audio track 1 is playing then on pausing the app crashes because of a null pointer exception due to mediaPlayer2 and the opposite happens when track 2 is playing.
Here's the code from the 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);
}
}
And the error(s)
Error 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)
Error 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)
Upvotes: 1
Views: 735