美文网首页Android 杂货铺精选
记录 Android WebView 开发过程的坑和解决方法

记录 Android WebView 开发过程的坑和解决方法

作者: 無名小子的杂货铺 | 来源:发表于2017-10-22 15:59 被阅读760次

    这篇主要总结 WebView 相关的疑难 bug,由于 Android 版本严重碎片化,在使用 WebView 的时候也会遇到各种个样的坑,特别是 4.4 之后更换了 WebView 内核,4.2 以下有部分漏洞,所以想把经历过的 WebView 这些坑记录下来,仅供参考。

    1、

    java.lang.NullPointerException
        at android.webkit.AccessibilityInjector$TextToSpeechWrapper$1.onInit(AccessibilityInjector.java:753)
        at android.speech.tts.TextToSpeech.dispatchOnInit(TextToSpeech.java:640)
        at android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:619)
        at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:553)
        at android.webkit.AccessibilityInjector$TextToSpeechWrapper.<init>(AccessibilityInjector.java:676)
        at android.webkit.AccessibilityInjector.addTtsApis(AccessibilityInjector.java:480)
        at android.webkit.AccessibilityInjector.addAccessibilityApisIfNecessary(AccessibilityInjector.java:168)
        at android.webkit.AccessibilityInjector.onPageStarted(AccessibilityInjector.java:340)
        at android.webkit.WebViewClassic.onPageStarted(WebViewClassic.java:4480)
        at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:366)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5407)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        at dalvik.system.NativeStart.main(Native Method)
    

    此问题在4.2.1和4.2.2比较集中,关闭辅助功能,google 下很多结果都是一样的。

    修复方法:在初始化 WebView 时调用disableAccessibility方法即可。

    public static void disableAccessibility(Context context) {
            if (Build.VERSION.SDK_INT == 17/*4.2 (Build.VERSION_CODES.JELLY_BEAN_MR1)*/) {
                if (context != null) {
                    try {
                        AccessibilityManager am = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
                        if (!am.isEnabled()) {
                            //Not need to disable accessibility
                            return;
                        }
    
                        Method setState = am.getClass().getDeclaredMethod("setState", int.class);
                        setState.setAccessible(true);
                        setState.invoke(am, 0);/**{@link AccessibilityManager#STATE_FLAG_ACCESSIBILITY_ENABLED}*/
                    } catch (Exception ignored) {
                        ignored.printStackTrace();
                    }
                }
            }
        }
    

    2、

    AndroidRuntimeException: android.content.pm.PackageManager$NameNotFoundException: com.google.android.webview
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4604)
        at android.app.ActivityThread.access$1500(ActivityThread.java:154)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1389)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5302)
        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:916)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711)
    Caused by: android.util.AndroidRuntimeException: android.content.pm.PackageManager$NameNotFoundException: com.google.android.webview
        at android.webkit.WebViewFactory.getFactoryClass(WebViewFactory.java:174)
        at android.webkit.WebViewFactory.getProvider(WebViewFactory.java:109)
        at android.webkit.WebView.getFactory(WebView.java:2194)
        at android.webkit.WebView.ensureProviderCreated(WebView.java:2189)
        at android.webkit.WebView.setOverScrollMode(WebView.java:2248)
        at android.view.View.<init>(View.java:3588)
        at android.view.View.<init>(View.java:3682)
        at android.view.ViewGroup.<init>(ViewGroup.java:497)
        at android.widget.AbsoluteLayout.<init>(AbsoluteLayout.java:55)
        at android.webkit.WebView.<init>(WebView.java:544)
        at android.webkit.WebView.<init>(WebView.java:489)
        at android.webkit.WebView.<init>(WebView.java:472)
        at android.webkit.WebView.<init>(WebView.java:459)
        at android.webkit.WebView.<init>(WebView.java:449)
    

    现象:在创建 WebView 时崩溃,跟进栈信息,我们需要在 setOverScrollMode 方法上加异常保护处理

    修复方法:

    try {
        super.setOverScrollMode(mode);
    } catch (Throwable e) {
        e.printStackTrace();
    }  
    

    不过上面捕获的异常范围有点广,在github上找到一个更全面的修复方法

    try{
        super.setOverScrollMode(mode);
    } catch(Throwable e){
            String messageCause = e.getCause() == null ? e.toString() : e.getCause().toString();
    String trace = Log.getStackTraceString(e);
    if (trace.contains("android.content.pm.PackageManager$NameNotFoundException")
      || trace.contains("java.lang.RuntimeException: Cannot load WebView")
        || trace.contains("android.webkit.WebViewFactory$MissingWebViewPackageException: Failed to load WebView provider: No WebView installed")) {
          e.printStackTrace();
        }else{
          throw e;
        }        
    }
    

    3、

    at android.webkit.BrowserFrame.nativeLoadUrl(Native Method)
    System.err:     at android.webkit.BrowserFrame.loadUrl(BrowserFrame.java:279)
    System.err:     at android.webkit.WebViewCore.loadUrl(WebViewCore.java:2011)
    System.err:     at android.webkit.WebViewCore.access$1900(WebViewCore.java:57)
    System.err:     at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1303)
    System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
    System.err:     at android.os.Looper.loop(Looper.java:137)
    System.err:     at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:812)
    System.err:     at java.lang.Thread.run(Thread.java:856)
    webcoreglue: *** Uncaught exception returned from Java call!
    System.err: java.lang.NullPointerException
    System.err:     at android.webkit.WebViewClassic.clearView(WebViewClassic.java:2868)
    System.err:     at android.webkit.WebViewCore.setupViewport(WebViewCore.java:2497)
    System.err:     at android.webkit.WebViewCore.updateViewport(WebViewCore.java:2479)
    System.err:     at android.webkit.BrowserFrame.nativeLoadUrl(Native Method)
    System.err:     at android.webkit.BrowserFrame.loadUrl(BrowserFrame.java:279)
    System.err:     at android.webkit.WebViewCore.loadUrl(WebViewCore.java:2011)
    System.err:     at android.webkit.WebViewCore.access$1900(WebViewCore.java:57)
    System.err:     at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1303)
    System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
    System.err:     at android.os.Looper.loop(Looper.java:137)
    

    这个bug是在某些设备上发生的,是在调用webView.destroy() 之前调用了loadurl操作发生的,也不是毕现问题,所以只能跟进源码查看,
    在清空 webview destroy 时,调用清理方法,内部可能时机有问题,会出现,WebViewClassic 中 mWebViewCore 对象为null,其内部为handler消息机制。

    修复方法:

    public void logdUrl(final String url) {
        try {
            super.loadUrl(url);
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    相关文章

      网友评论

      本文标题:记录 Android WebView 开发过程的坑和解决方法

      本文链接:https://www.haomeiwen.com/subject/xawguxtx.html