K.O*_*.Os 7 android android-webview kotlin
我试图用自定义HTML字符串填充我的WebView并尝试在未加载时显示进度,并在完成时隐藏它.
这是我的代码:
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
}
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误,这不是指向我的代码的任何行:
E/AndroidRuntime:致命异常:主要
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)
Run Code Online (Sandbox Code Playgroud)
小智 29
我也有同样的问题.
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter favicon
at com.haoyong.szzc.module.share.view.activity.WebActivity$MyWebViewClient.onPageStarted(WebActivity.kt:0)
at com.android.webview.chromium.WebViewContentsClientAdapter.onPageStarted(WebViewContentsClientAdapter.java:495)
at com.android.org.chromium.android_webview.AwContentsClientCallbackHelper$MyHandler.handleMessage(AwContentsClientCallbackHelper.java:122)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5313)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1116)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:809)
Run Code Online (Sandbox Code Playgroud)
我只是这样做: 改变
override fun onPageStarted(view: WebView, url: String, favicon: Bitmap) {
super.onPageStarted(view, url, favicon)
}
Run Code Online (Sandbox Code Playgroud)
至
override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
}
Run Code Online (Sandbox Code Playgroud)
因为不允许在Kotlinlang中使用null参数.只是将位图更改为位图?然后它会运作良好.希望这可以帮助其他人.
系统通过,null
favicon
但被定义为不可为空的Kotlin类型。您可以通过更改签名favicon: Bitmap?
使其可为空来修复它。
问题是onPageStarted
(系统)调用了传递参数null
值的方法favicon
。当某些Kotlin代码与Java代码互操作时,可能会发生这种情况(默认情况下,您应该获得可为空的对象)。
任何平台类型(例如,来自Java的任何对象)都可以null
是Java,因为Java没有特殊的表示法来告知某些事物可以或不能null
。因此,当您在Kotlin中使用平台类型时,可以选择以下两种方式之一:
按原样使用它;在这种情况下的后果是(根据文档)
对此类类型放宽了空检查,因此对它们的安全保证与Java中的相同
因此,您可能会得到NullPointerException
s,如以下示例所示:
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
}
Run Code Online (Sandbox Code Playgroud)将其强制转换为特定类型(可为空或不可为空);在这种情况下,Kotlin编译器会将其视为“普通” Kotlin类型。例:
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
}
Run Code Online (Sandbox Code Playgroud)
但是,如果您强制使用不可为null的类型然后又返回,则会抛出异常null
。例:
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
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您可以“安全使用”并将变量强制为可为空–这将永远不会失败,但是可能会使代码更难于阅读:
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!"
}
Run Code Online (Sandbox Code Playgroud)您可以在代码中使用后一个示例来解决bitmap
null问题:
override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3890 次 |
最近记录: |