Strange NPE deep inside MediaSessionCompat

314 views Asked by At

I have seen similar questions posted about MediaSessionCompat but they are all pretty old, in fact most predate AndroidX and they point at it being fixed in newer versions.

This crash is happening with androidx.media:media:1.2.1 on various phones from Android 8 through 11. It is always MediaSessionImplApi21. Works fine for me on every phone I've tried.

This issue is new for me as of my latest release. The two big changes I made are that I added a progress bar to my MediaStyle notification and that I upgraded to 1.2.1 from 1.0.0.

This is the exception:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.media.session.MediaSessionCompat$Callback android.support.v4.media.session.MediaSessionCompat$MediaSessionImplApi21.getCallback()' on a null object reference
       at android.support.v4.media.session.MediaSessionCompat$Callback$MediaSessionCallbackApi21.getSessionImplIfCallbackIsSet(MediaSessionCompat.java:1907)
       at android.support.v4.media.session.MediaSessionCompat$Callback$MediaSessionCallbackApi21.onMediaButtonEvent(MediaSessionCompat.java:1597)
       at android.media.session.MediaSession$CallbackMessageHandler.handleMessage(MediaSession.java:1471)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:214)
       at android.app.ActivityThread.main(ActivityThread.java:7094)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)

If my Callback wasn't set them a lot of things wouldn't work.

Also my code looks like this:

MediaSessionCompat mediaSessionCompat = new MediaSessionCompat(context, TAG);
mediaSessionCompat.setCallback(mediaSessionCallback);

So there's really no room to not set the Callback.

Any idea what it could be?

From looking at the code and where it crashed:

        private MediaSessionImplApi21 getSessionImplIfCallbackIsSet() {
            MediaSessionImplApi21 sessionImpl;
            synchronized (mLock) {
                sessionImpl = (MediaSessionImplApi21) mSessionImpl.get();
            }
            return MediaSessionCompat.Callback.this == sessionImpl.getCallback()
                    ? sessionImpl : null;
        }

It is crashing on sessionImpl.getCallback(). Which probably means it either got recycled because mSessionImpl is a WeakReference or it was set to null to begin with.

1

There are 1 answers

0
casolorz On BEST ANSWER

Turns out it was a bug and has now been patched and will hopefully be out on 1.2.2 https://issuetracker.google.com/issues/178694750