java.lang.IllegalArgumentException:对于Kotlin和WebView,指定为non-null的参数为null [英] java.lang.IllegalArgumentException: Parameter specified as non-null is null for Kotlin and WebView

查看:461
本文介绍了java.lang.IllegalArgumentException:对于Kotlin和WebView,指定为non-null的参数为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用自定义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:致命异常:主要

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

系统通过null favicon,但它被定义为不可为空的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,如以下示例所示:

Hence you may get NullPointerExceptions, 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的类型却又返回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,指定为non-null的参数为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆