简体   繁体   English

Kotlin 中的 DialogFragment.onDismiss() 空指针异常

[英]DialogFragment.onDismiss() null pointer exception in Kotlin

This is Kotlin, not Java, FYI.这是 Kotlin,不是 Java,仅供参考。

I have a DialogFragment subclass that I'm using, and I'm getting some crashes when it's dismissed.我有一个正在使用的 DialogFragment 子类,当它被关闭时我遇到了一些崩溃。 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.崩溃发生在重写的onDismiss()函数内部,作为自动 Kotlin Intrinstics.checkParameterIsNotNull()调用。 Here is the decompiled Kotlin->Java code from my dialog subclass:这是我的对话框子类中反编译的 Kotlin->Java 代码:

 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.这是 DialogInterface 参数,它发生在我的任何代码运行之前,所以我无法采取任何措施来阻止它。 It's not possible to put a check before this, because the Kotlin compiler inserts the null-check before any of my code.不可能在此之前进行检查,因为 Kotlin 编译器会在我的任何代码之前插入 null 检查。

I am dismissing the dialog by calling this, inside the owning Activity's onStop()我正在通过在拥有 Activity 的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 .看起来这不应该发生,因为接口指定它不能为null

This may be due to this bug on the Android issue tracker, which was fixed in version androidx.fragment version 1.2.0.这可能是由于 Android 问题跟踪器上的这个错误,该错误已在版本 androidx.fragment 版本 1.2.0 中修复。 Updating to that version of androidx.fragment should resolve the issue.更新到该版本的 androidx.fragment 应该可以解决问题。

Specifically it appears to be an issue with the timing of cancel/dismiss callbacks when the dialog has already been destroyed.具体来说,当对话框已经被销毁时,取消/关闭回调的时间似乎是一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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