java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.

java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter favicon

WebView在java上无错误,移植到kotlin报错问题记录,希望能帮到你

java中的写法

        webView = findViewById(R.id.web_view);
        initWebView();
        if (savedInstanceState != null) {
            webView.restoreState(savedInstanceState);
        } else {
            webView.loadUrl("https://baidu.com/");
        }
        webView.setWebViewClient(new WebViewClient() {

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

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });

kotlin中的写法

        webView = findViewById(R.id.web_view)
        initWebView()
        if (savedInstanceState != null) {
            webView!!.restoreState(savedInstanceState)
        } else {
            webView!!.loadUrl("https://baidu.com/")
        }
        webView!!.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                view.loadUrl(url)
                return true
            }

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

            override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)
            }
        }

两种写法一毛一样,但是使用kotlin运行就出现了崩溃问题,

崩溃日志:
    java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter favicon
        at com.xxx.xxx.xxxxActivity$onCreate$1.onPageStarted(Unknown Source:12)
        at Ob.c(chromium-SystemWebView.apk-default-410412053:2)
        at to.handleMessage(chromium-SystemWebView.apk-default-410412053:162)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7705)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:952)

原因就是因为有一个方法里面的参数需要设置为可以空的才可以

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

加上可为空之后,运行正常。希望上述处理能帮到你。

你可能感兴趣的:(Android,kotlin,java,android)