java.lang.IllegalArgumentException: Parameter specified as non-null is null for Kotlin and WebView

后端 未结 3 824
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-07 01:09

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.

Here is my code:

<         


        
3条回答
  •  Happy的楠姐
    2020-12-07 01:54

    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.

    Full response

    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).

    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)

      Null-checks are relaxed for such types, so that safety guarantees for them are the same as in Java

      Hence you may get NullPointerExceptions, like in the following example:

      fun main(args: Array) {
          val array = Vector() // 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
      }
      
    • 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) {
          val array = Vector() // 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
      }
      

      However, this will throw an exception if you enforce a non-nullable type but then get back null. Example:

      fun main(args: Array) {
          val array = Vector() // 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) {
          val array = Vector() // 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!"
      }
      

    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?) {
        ...
    }
    

提交回复
热议问题