java.lang.IllegalArgumentException: 指定为非空的参数对于 Kotlin 和 WebView 为空

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

我正在尝试使用自定义 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
  }
}

我收到此错误,它没有指向我的代码的任何行:

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.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 但它被定义为 non-nullable Kotlin 类型。您可以通过将签名更改为 favicon: Bitmap? 使其可为空来修复它。

完全响应

问题是方法 onPageStarted 被(由系统)调用,为 favicon 参数传递了 null 值。当有一些 Kotlin 代码与 Java 代码互操作时可能会发生这种情况(默认情况下你应该获得可为空的对象)。

任何平台类型(例如来自 Java 的任何对象)都可以是 null,因为 Java 没有特殊的符号来表明某些东西可以或不可以 null.因此,当您在 Kotlin 中使用平台类型时,您可以选择:

  • 使用它"as-is";这种情况下的结果是(来自文档)

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

    因此您可能会得到 NullPointerExceptions,如下例所示:

    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
    }
    
  • 将其转换为特定类型(可为空或 non-nullable);在这种情况下,Kotlin 编译器会将其视为 "normal" 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
    }
    

    但是,如果您强制执行 non-nullable 类型但随后返回 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
    }
    

    在这种情况下,您可以 "play it safe" 并强制变量可以为空——这永远不会失败,但可能会使代码更难阅读:

    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 为空的情况:

override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
    ...
}

我也遇到了同样的问题

 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)

我就是这样操作的: 改变

override fun onPageStarted(view: WebView, url: String, favicon: Bitmap) {
            super.onPageStarted(view, url, favicon)
        }

 override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
            super.onPageStarted(view, url, favicon)
        }

因为Kotlinlang中不允许使用null参数。只是将位图更改为位图?然后它会很好地工作。希望这可以帮助其他人。

private void initWebView() {
    webView.setWebChromeClient(new MyWebChromeClient(getActivity()));
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                progressBar.setVisibility(View.VISIBLE);
                getActivity().invalidateOptionsMenu();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                webView.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                progressBar.setVisibility(View.GONE);
                getActivity().invalidateOptionsMenu();
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                progressBar.setVisibility(View.GONE);
                getActivity().invalidateOptionsMenu();
            }
        });
        webView.clearCache(true);
        webView.clearHistory();
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setHorizontalScrollBarEnabled(false);
        webView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {

                if (event.getPointerCount() > 1) {
                    //Multi touch detected
                    return true;
                }

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        // save the x
                        m_downX = event.getX();
                    }
                    break;

                    case MotionEvent.ACTION_MOVE:
                    case MotionEvent.ACTION_CANCEL:
                    case MotionEvent.ACTION_UP: {
                        // set x so that it doesn't move
                        event.setLocation(m_downX, event.getY());
                    }
                    break;
                }

                return false;
            }
        });
    }

    private class MyWebChromeClient extends WebChromeClient {
        Context context;

        public MyWebChromeClient(Context context) {
            super();
            this.context = context;
        }

    }

内部创建---

initWebView()

binding.webView.loadUrl(urlName)

只需尝试一下即可获得流畅的 Web 视图 URL 加载。