AgentWeb在Kotlin开发使用中出现WebView的onPageStarted中favicon为空导致崩溃

版权声明:博文网络共享,尊重知识原创。 https://blog.csdn.net/binglumeng/article/details/86228344

最新项目中使用了AgentWeb的一个WebView封装库,使用kotlin语言开发时候出现了IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter favicon的问题,而在Java语言情况下不会出现崩溃。

  • 场景:

    kotlin开发环境,使用AgentWeb,在WebActivity的onCreate中,AgentWeb初始配置如下

     //初始化AgentWeb对象
            mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(
                    mLinearLayout, LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
                     )
                 )
                 .useDefaultIndicator()
                 .setWebChromeClient(mWebChromeClient)
                 .setWebViewClient(mWebViewClient)//注意这里!!!!!!!!
                 .setMainFrameErrorView(R.layout.agentweb_error_page, -1)
                 .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
     		.setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)//打开其他应用时,弹窗咨询用户是否前往其他应用
                 .interceptUnkownUrl() //拦截找不到相关页面的Scheme
                 .createAgentWeb()
                 .ready()
                 .go("http://www.google.com")
    
  • 问题

    打开WebActivity会直接崩溃,错误日志如下

    java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter favicon
            at com.zhiwei.services.webapp.BaseWebActivity$mWebViewClient$1.onPageStarted(Unknown Source:12)
            at com.just.agentweb.WebViewClientDelegate.onPageStarted(WebViewClientDelegate.java:80)
            at com.just.agentweb.DefaultWebClient.onPageStarted(DefaultWebClient.java:466)
            at xq.b(SourceFile:219)
            at alW.handleMessage(SourceFile:20)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:193)
    

    问题清晰描述为在onPagestarted函数中favicon字段为空,kotlin本身调用时候认为为非空的数据,现在为空,所以引起崩溃。

  • 解决方案

    • 如不需要特殊处理,可以移除setWebViewClient这个初始化配置

    • 若需要使用setWebViewClient,则需要同时设置一个useMiddlewareWebClient

      .setWEbViewClient(mWebViewClient)
      .useMiddlewareWebClient(object:MiddlewareWebClientBase(){
          //可以抽取出去,定义一个实现类也行,这里使用了匿名内部类
          override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                          //定义一个bitmap,避免favicon为空,引起的崩溃
                          val ff = favicon ?: ImageUtils.drawable2Bitmap(AppUtils.getAppIcon())
                          super.onPageStarted(view, url, ff)
                      }
      })
      
    • 修改源码DefaultWebClient

      @Override
      	public void onPageStarted(WebView view, String url, Bitmap favicon) {
      		//在这里处理favicon为空,defaultWebClient内部有Activity的弱引用
              //可以取Activity获取到resource,来构建一个bitmap给faicon,
              //或者WebIconDatabase.getInstance().open(getDir("icons", MODE_PRIVATE).getPath());设置web的图标
      		if (!mWaittingFinishSet.contains(url)) {
      			mWaittingFinishSet.add(url);
      		}
      		super.onPageStarted(view, url, favicon);
      
      	}
      
  • 原因分析:kotlin语言中若未声明bitmap:Bitmap?为可null类型,则断定就是非空,所以如果传递的值,是null值,就会崩溃。类似Java中@NonNull标记,而AgentWebDefaultWebClientWebViewClientdelegateonPageStarted函数,都是java文件,里面未做favicon的非空判断。(似乎就是android webview的问题

猜你喜欢

转载自blog.csdn.net/binglumeng/article/details/86228344
今日推荐