skydoves / Balloon

:balloon: Modernized and sophisticated tooltips, fully customizable with an arrow and animations for Android.
https://skydoves.github.io/libraries/balloon/html/balloon/com.skydoves.balloon/index.html
Apache License 2.0
3.75k stars 291 forks source link

NPE in `canShowBalloonWindow` Due to Null `mAttachInfo` #730

Closed ogaclejapan closed 1 month ago

ogaclejapan commented 1 month ago

Please complete the following information:

Describe the Bug:

Since the change in #703, the app crashes if mAttachInfo inside a View is null.

java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.os.IBinder.isBinderAlive()' on a null object reference
                                                                                                        at com.skydoves.balloon.Balloon.canShowBalloonWindow(Balloon.kt:923)
                                                                                                        at com.skydoves.balloon.Balloon.show(Balloon.kt:796)

Expected Behavior:

The code should be modified as follows:

  private fun canShowBalloonWindow(anchor: View): Boolean {
    ...

    // We should check the anchor view is attached to the parent's window.
    if (!anchor.isAttachedToWindow) return false 

    // We should check if the anchor view's window token is valid.
    return anchor.windowToken.isBinderAlive
  }

The isAttachedToWindow function compares mAttachInfo != null, so it is preferable to reference the windowToken only after this check.

View.java:

    public IBinder getWindowToken() {
        return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
    }

    public boolean isAttachedToWindow() {
        return mAttachInfo != null;
    }
skydoves commented 1 month ago

Hey @ogaclejapan, the new version 1.6.9 has been released and it resolves this issue. Thank you for your report!