Balloon icon indicating copy to clipboard operation
Balloon copied to clipboard

NPE in `canShowBalloonWindow` Due to Null `mAttachInfo`

Open ogaclejapan opened this issue 1 year ago • 0 comments

Please complete the following information:

  • Library Version 1.6.8
  • Affected Device(s) Uknown

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;
    }

ogaclejapan avatar Oct 15 '24 10:10 ogaclejapan