简体   繁体   中英

DialogFragment.onDismiss() null pointer exception in Kotlin

This is Kotlin, not Java, FYI.

I have a DialogFragment subclass that I'm using, and I'm getting some crashes when it's dismissed. It doesn't happen every time, and I can't seem to find a pattern to it. The crash is happening inside the overridden onDismiss() function, as an automatic Kotlin Intrinstics.checkParameterIsNotNull() call. Here is the decompiled Kotlin->Java code from my dialog subclass:

 public void onDismiss(@NotNull DialogInterface dialog) {
   Intrinsics.checkParameterIsNotNull(dialog, "dialog");
   super.onDismiss(dialog);
}

And here is the crash message:

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter dialog
at com.foo.MyDialogFragment.onDismiss(Unknown Source:2)

This is the DialogInterface parameter, and it is happening before any of my code runs, so I cannot do anything to prevent it. It's not possible to put a check before this, because the Kotlin compiler inserts the null-check before any of my code.

I am dismissing the dialog by calling this, inside the owning Activity's onStop()

myDialog?.dismissAllowingStateLoss()

Any idea what is causing this, and more importantly, what can I do? It seems like this should not ever be happening, because the interface specifies it cannot be null .

This may be due to this bug on the Android issue tracker, which was fixed in version androidx.fragment version 1.2.0. Updating to that version of androidx.fragment should resolve the issue.

Specifically it appears to be an issue with the timing of cancel/dismiss callbacks when the dialog has already been destroyed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM