Skip to content

Kotlin使用agentweb引起WebView的onPageStarted中favicon为空导致崩溃 #490

@iOrchid

Description

@iOrchid
  • 场景:

    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的问题

综合建议:还是希望作者可以处理一下faviconnull问题,便于更多的其他开发者使用kotlin开发时候,减少这个麻烦的处理。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions