java.lang.IllegalArgumentException:对于 Kotlin 和 WebView,指定为非 null 的参数为 null [英] java.lang.IllegalArgumentException: Parameter specified as non-null is null for Kotlin and WebView
问题描述
我正在尝试使用自定义 HTML 字符串填充我的 WebView,并尝试在未加载时显示进度,并在完成时隐藏它.
I am trying to populate my WebView with custom HTML string and trying to show progress when it is not loaded, and hide it when finished.
这是我的代码:
webView.settings.javaScriptEnabled = true
webView.loadDataWithBaseURL(null, presentation.content, "text/html", "utf-8", null)
webView.webViewClient = object : WebViewClient() {
override fun onPageStarted(view: WebView, url: String, favicon: Bitmap) {
super.onPageStarted(view, url, favicon)
webViewProgressBar.visibility = ProgressBar.VISIBLE
webView.visibility = View.INVISIBLE
}
override fun onPageCommitVisible(view: WebView, url: String) {
super.onPageCommitVisible(view, url)
webViewProgressBar.visibility = ProgressBar.GONE
webView.visibility = View.VISIBLE
}
}
我收到此错误,它没有指向我的代码的任何行:
I am getting this error, which is not pointing to any line of my code:
E/AndroidRuntime:致命异常:main
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter favicon
at com.hidglobal.tmt.app.mobiBadge.ui.presentation.PresentationActivity$showPresentation$1.onPageStarted(PresentationActivity.kt)
at com.android.webview.chromium.WebViewContentsClientAdapter.onPageStarted(WebViewContentsClientAdapter.java:215)
at org.chromium.android_webview.AwContentsClientCallbackHelper$MyHandler.handleMessage(AwContentsClientCallbackHelper.java:20)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
推荐答案
TL;博士
系统传递一个 null
favicon
但它被定义为不可为 null 的 Kotlin 类型.您可以通过将签名更改为 favicon: Bitmap?
使其可以为空来修复它.
TL; DR
The system passes a null
favicon
but it is defined as a non-nullable Kotlin type. You can fix it by changing the signature to favicon: Bitmap?
to make it nullable.
问题是方法 onPageStarted
被调用(由系统)传递 favicon
参数的 null
值.当某些 Kotlin 代码与 Java 代码互操作时可能会发生这种情况(默认情况下,您应该获得可为空的对象).
The issue is that method onPageStarted
is called (by the system) passing a null
value for the favicon
parameter. This may happen when there is some Kotlin code that is interoperating with Java code (by default you should get nullable objects).
任何平台类型(例如来自 Java 的任何对象)都可以是 null
,因为 Java 没有特殊的符号来说明某些东西可以或不可以是 null
.因此,当您在 Kotlin 中使用平台类型时,您可以选择:
Any platform type (e.g. any objects coming from Java) can be null
, because Java has no special notation to tell that something can or cannot be null
. For that reason when you use platform types in Kotlin you can choose to either:
按原样"使用;在这种情况下的结果是(来自文档)
use it "as-is"; the consequence in such case is that (from documentation)
对此类类型放宽空检查,因此它们的安全保证与 Java 中的相同
Null-checks are relaxed for such types, so that safety guarantees for them are the same as in Java
因此,您可能会收到 NullPointerException
s,如下例所示:
Hence you may get NullPointerException
s, like in the following example:
fun main(args: Array<String>) {
val array = Vector<String>() // we need to Vector as it's not mapped to a Kotlin type
array.add(null)
val retrieved = array[0]
println(retrieved.length) // throws NPE
}
将其强制转换为特定类型(可为空或不可为空);在这种情况下,Kotlin 编译器会将其视为正常"的 Kotlin 类型.示例:
cast it to a specific type (either nullable or non-nullable); in this case the Kotlin compiler will treat it as a "normal" Kotlin type. Example:
fun main(args: Array<String>) {
val array = Vector<String>() // we need to Vector as it's not mapped to a Kotlin type
array.add("World")
val retrieved: String = array[0] // OK, as we get back a non-null String
println("Hello, $retrieved!") // OK
}
但是,如果您强制使用不可为空的类型,然后又返回 null
,这将引发异常.示例:
However, this will throw an exception if you enforce a non-nullable type but then get back null
. Example:
fun main(args: Array<String>) {
val array = Vector<String>() // we need to Vector as it's not mapped to a Kotlin type
array.add(null)
val retrieved: String = array[0] // we force a non-nullable type but get null back -> throws NPE
println("Hello, World!") // will not reach this instruction
}
在这种情况下,您可以谨慎行事"并强制变量可以为空——这永远不会失败,但可能会使代码更难阅读:
In such case you can "play it safe" and enforce the variable to be nullable – this will never fail, but could make the code harder to read:
fun main(args: Array<String>) {
val array = Vector<String>() // we need to Vector as it's not mapped to a Kotlin type
array.add(null)
val retrieved: String? = array[0] // OK since we use a nullable type
println("Hello, $retrieved!") // prints "Hello, null!"
}
您可以在代码中使用后一个示例来处理 bitmap
为空的情况:
You can use the latter example in your code to cope with the bitmap
being null:
override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
...
}
这篇关于java.lang.IllegalArgumentException:对于 Kotlin 和 WebView,指定为非 null 的参数为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!