【问题标题】:Android WebView: crash after url loadingAndroid WebView:url加载后崩溃
【发布时间】:2016-04-26 01:13:02
【问题描述】:

使用我的 webView 加载 url 时,应用程序在几秒钟后崩溃(没有错误日志...)。

我的代码:

 wv = new WebView(this);
        wv.clearCache(true);
        wv.clearHistory();
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        wv.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent,
                                        String contentDisposition, String mimetype, long contentLength) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setType(mimetype);
                intent.setData(Uri.parse(url));
                startActivity(intent);
            }
        });


        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                // TODO change for other domains
                URL nextUrl;
                try {
                    nextUrl = new URL(url.toString());
                }catch (MalformedURLException e){
                    nextUrl = null;
                }

                if(nextUrl !=null && nextUrl.getHost().toString().equals(DOMAIN)) {
                    Toast.makeText(mContext, nextUrl.getHost().toString(), Toast.LENGTH_SHORT).show();
                    view.loadUrl(url);
                    return false;

                }else{
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(browserIntent);
                    return true;
                }

            }
        });

        wv.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                MainActivity.this.setProgress(progress * 1000);
            }
            public boolean onConsoleMessage(ConsoleMessage cm) {
                Log.d("MyProject: WebView: ", cm.message() + " -- From line "
                    + cm.lineNumber() + " of "
                    + cm.sourceId() );
            return true;
        }
        });

        wv.loadUrl(URL);
        setContentView(wv);

日志:

01-20 18:00:50.798    7233-7288/ my.appli.com I/dalvikvm﹕ "WebViewCoreThread" prio=5 tid=12 NATIVE
01-20 18:00:50.798    7233-7288/ my.appli.com I/dalvikvm﹕ | group="main" sCount=0 dsCount=0 obj=0x419a0be0 self=0x68f6c750
01-20 18:00:50.798    7233-7288/ my.appli.com I/dalvikvm﹕ | sysTid=7288 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=1773204176
01-20 18:00:50.799    7233-7288/ my.appli.com I/dalvikvm﹕ | state=R schedstat=( 0 0 0 ) utm=2326 stm=119 core=0
01-20 18:00:50.841    7233-7288/ my.appli.com I/dalvikvm﹕ #00  pc 000012a0  /system/lib/libcorkscrew.so (unwind_backtrace_thread+27)
01-20 18:00:50.841    7233-7288/ my.appli.com I/dalvikvm﹕ #01  pc 0006235c  /system/lib/libdvm.so (dvmDumpNativeStack(DebugOutputTarget const*, int)+35)
01-20 18:00:50.841    7233-7288/ my.appli.com I/dalvikvm﹕ #02  pc 000561bc  /system/lib/libdvm.so (dvmDumpThreadEx(DebugOutputTarget const*, Thread*, bool)+303)
01-20 18:00:50.841    7233-7288/ my.appli.com I/dalvikvm﹕ #03  pc 00056256  /system/lib/libdvm.so (dvmDumpThread(Thread*, bool)+25)
01-20 18:00:50.841    7233-7288/ my.appli.com I/dalvikvm﹕ #04  pc 000478c8  /system/lib/libdvm.so (dvmDebuggerSignalHandler(int, siginfo*, void*)+15)
01-20 18:00:50.841    7233-7288/ my.appli.com I/dalvikvm﹕ at android.webkit.JWebCoreJavaBridge.sharedTimerFired(Native Method)
01-20 18:00:50.841    7233-7288/ my.appli.com I/dalvikvm﹕ at android.webkit.JWebCoreJavaBridge.fireSharedTimer(JWebCoreJavaBridge.java:92)
01-20 18:00:50.841    7233-7288/ my.appli.com I/dalvikvm﹕ at android.webkit.JWebCoreJavaBridge.handleMessage(JWebCoreJavaBridge.java:108)
01-20 18:00:50.841    7233-7288/ my.appli.com I/dalvikvm﹕ at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 18:00:50.841    7233-7288/ my.appli.com I/dalvikvm﹕ at android.os.Looper.loop(Looper.java:137)
01-20 18:00:50.841    7233-7288/ my.appli.com I/dalvikvm﹕ at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:900)
01-20 18:00:50.841    7233-7288/ my.appli.com I/dalvikvm﹕ at java.lang.Thread.run(Thread.java:856)
01-20 18:00:50.841    7233-7288/ my.appli.com I/dalvikvm﹕ [ 01-20 18:00:50.841  7233: 7288 F/libc     ]
    Fatal signal 11 (SIGSEGV) at 0x0000001c (code=1), thread 7288 (WebViewCoreThre)

我在尝试使用我的 android 设备 (4.1) 的默认浏览器加载相同的 URL 时遇到了同样的问题(崩溃)。

我要加载的网址是:

http://presentbox.jp

感谢您的帮助。

--- 编辑 1

我用最近的 android 手机 (4.4) 尝试过,应用程序没有崩溃。

出于调试目的,我删除了网站的所有重要部分(图像、js、...),但应用程序在向下滚动后仍然崩溃。

【问题讨论】:

  • 您是否尝试添加加载栏?以便网页可以先完全加载
  • 感谢您的回复。是的,我已经添加了一个 progressDialog,直到页面加载但仍然崩溃......
  • @johann 您的网站上是否有使用 SSL 证书的相关内容?
  • @user1140237 : 我的网站没有 SSL 认证

标签: javascript java android webview


【解决方案1】:

我在运行 Android 6.0.x 的 Nexus 5X 和 Nexus 6P 设备的网络视图中加载了谷歌地图对象/或任何 2d 画布对象后遇到了类似的问题。

我似乎认为这与此处报告的硬件错误有关: https://code.google.com/p/chromium/issues/detail?id=541145

在即将发布的 webview 版本中似乎正在提供修复或解决方法。

在新的 webview 版本到来之前,我不得不接受将 webview 设置为软件渲染的建议修复。这会使地图变慢,但至少不会崩溃。

if(isNexus5xOr6p() && android.os.Build.VERSION.SDK_INT >= 11)
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

public boolean isNexus5xOr6p() {

     if(getDeviceName().contains("nexus 6p") || getDeviceName().contains("nexus 5x"))
                    return true;
                else
                    return false;
        }


public String getDeviceName() {
        String manufacturer = Build.MANUFACTURER;
        String model = Build.MODEL;
        if (model.startsWith(manufacturer)) {
            return model.toLowerCase();
        }
        return (manufacturer + " " + model).toLowerCase();
}

【讨论】:

    【解决方案2】:

    // 编辑:找到了!罪魁祸首在general.css第2540行:

    #head-search-form{display: block;margin: 30px 0;}
    

    更具体地说,是display: block 以某种方式使WebView 窒息。我不是一个网络开发人员,但用flexnone 交换值似乎不再导致崩溃(而且我无法分辨移动设备上的视觉差异)。希望对您有所帮助!


    我已经建立了一个小型测试项目,并且能够在(虚拟)Android 4.1 设备上重现崩溃。它似乎只发生在页面完全加载后滚动时。 IE。只要不开始滚动,您就可以毫无问题地展开菜单抽屉...

    我能够将罪魁祸首缩小到general.css。只要您不加载该样式表,页面就可以正常工作和滚动,但当然看起来不会很漂亮。

    由于general.css 计数超过 3300 行,我建议您通过修复the errors indicated by the W3C CSS Validator 来开始搜索实际原因。如果这不能解决问题,请开始禁用与照片网格相关的样式规则,尤其是任何涉及动画/转换的内容。如果我有时间,我自己也可以试试。

    仅供参考,这里有一个更广泛的堆栈跟踪。其他人也可以在那里找到更多指针。

    mh.test.webview A/libc: Fatal signal 11 (SIGSEGV) at 0x00001f08 (code=1), thread 11949 (WebViewCoreThre)
    I/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
    I/DEBUG: Build fingerprint: 'generic/vbox86p/vbox86p:4.1.1/JRO03S/eng.buildbot.20151117.133415:userdebug/test-keys'
    I/DEBUG: pid: 11930, tid: 11949, name: WebViewCoreThre  >>> mh.test.webview <<<
    I/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00001f08
    I/DEBUG:     eax b84902e4  ebx 9ba8b488  ecx b804b028  edx b83c0224
    I/DEBUG:     esi b8490360  edi b804b028
    I/DEBUG:     xcs 00000073  xds 0000007b  xes 0000007b  xfs 00000000  xss 0000007b
    I/DEBUG:     eip 00001f08  ebp 997b9748  esp 997b96dc  flags 00010296
    I/DEBUG:     #00  pc 00001f08  <unknown>
    I/DEBUG:     #01  pc 00344fd6  /system/lib/libwebcore.so (WebCore::RenderLayerCompositor::computeCompositingRequirements(WebCore::RenderLayer*, WTF::HashMap<WebCore::RenderLayer*, WebCore::IntRect, WTF::PtrHash<WebCore::RenderLayer*>, WTF::HashTraits<WebCore::RenderLayer*>, WTF::HashTraits<WebCore::IntRect> >*, WebCore::CompositingState&, bool&)+38)
    I/DEBUG:     #02  pc 003454bb  /system/lib/libwebcore.so (WebCore::RenderLayerCompositor::computeCompositingRequirements(WebCore::RenderLayer*, WTF::HashMap<WebCore::RenderLayer*, WebCore::IntRect, WTF::PtrHash<WebCore::RenderLayer*>, WTF::HashTraits<WebCore::RenderLayer*>, WTF::HashTraits<WebCore::IntRect> >*, WebCore::CompositingState&, bool&)+1291)
    I/DEBUG:     #03  pc 003454bb  /system/lib/libwebcore.so (WebCore::RenderLayerCompositor::computeCompositingRequirements(WebCore::RenderLayer*, WTF::HashMap<WebCore::RenderLayer*, WebCore::IntRect, WTF::PtrHash<WebCore::RenderLayer*>, WTF::HashTraits<WebCore::RenderLayer*>, WTF::HashTraits<WebCore::IntRect> >*, WebCore::CompositingState&, bool&)+1291)
    I/DEBUG:     #04  pc 003454bb  /system/lib/libwebcore.so (WebCore::RenderLayerCompositor::computeCompositingRequirements(WebCore::RenderLayer*, WTF::HashMap<WebCore::RenderLayer*, WebCore::IntRect, WTF::PtrHash<WebCore::RenderLayer*>, WTF::HashTraits<WebCore::RenderLayer*>, WTF::HashTraits<WebCore::IntRect> >*, WebCore::CompositingState&, bool&)+1291)
    I/DEBUG:     #05  pc 003454bb  /system/lib/libwebcore.so (WebCore::RenderLayerCompositor::computeCompositingRequirements(WebCore::RenderLayer*, WTF::HashMap<WebCore::RenderLayer*, WebCore::IntRect, WTF::PtrHash<WebCore::RenderLayer*>, WTF::HashTraits<WebCore::RenderLayer*>, WTF::HashTraits<WebCore::IntRect> >*, WebCore::CompositingState&, bool&)+1291)
    I/DEBUG:     #06  pc 00346c55  /system/lib/libwebcore.so (WebCore::RenderLayerCompositor::updateCompositingLayers(WebCore::CompositingUpdateType, WebCore::RenderLayer*)+213)
    I/DEBUG:     #07  pc 0020e727  /system/lib/libwebcore.so (WebCore::FrameView::layout(bool)+1159)
    I/DEBUG:     #08  pc 0068126f  /system/lib/libwebcore.so (WebCore::Document::updateLayout()+127)
    I/DEBUG:     #09  pc 0068bbda  /system/lib/libwebcore.so (WebCore::Document::updateLayoutIgnorePendingStylesheets()+90)
    I/DEBUG:     #10  pc 005ead53  /system/lib/libwebcore.so (WebCore::CSSComputedStyleDeclaration::getPropertyCSSValue(int, WebCore::EUpdateLayout) const+467)
    I/DEBUG:     #11  pc 005f35e9  /system/lib/libwebcore.so (WebCore::CSSComputedStyleDeclaration::getPropertyCSSValue(int) const+41)
    I/DEBUG:     #12  pc 005e68d7  /system/lib/libwebcore.so (WebCore::CSSComputedStyleDeclaration::getPropertyValue(int) const+55)
    I/DEBUG:     #13  pc 006297f8  /system/lib/libwebcore.so (WebCore::CSSStyleDeclaration::getPropertyValue(WTF::String const&)+88)
    I/DEBUG:     #14  pc 004d9475  /system/lib/libwebcore.so (WebCore::CSSStyleDeclarationInternal::getPropertyValueCallback(v8::Arguments const&)+181)
    I/DEBUG:     #15  pc 000efdd4  <unknown>
    I/DEBUG:     #16  pc 0005cf6b  <unknown>
    I/DEBUG:     #17  pc 000bb32f  <unknown>
    I/DEBUG:     #18  pc 00003b41  <unknown>
    I/DEBUG:     #19  pc 000f47ce  <unknown>
    I/DEBUG:     #20  pc 0001a5a3  <unknown>
    I/DEBUG:     #21  pc 000065e3  <unknown>
    I/DEBUG:     #22  pc 00003b41  <unknown>
    I/DEBUG:     #23  pc 00024c59  <unknown>
    I/DEBUG:     #24  pc 00024dad  <unknown>
    I/DEBUG:     #25  pc 00003b41  <unknown>
    I/DEBUG:     #26  pc 0001a59c  <unknown>
    I/DEBUG:     #27  pc 000d7172  <unknown>
    I/DEBUG:     #28  pc 0001a5a3  <unknown>
    I/DEBUG:     #29  pc 000d40ed  <unknown>
    I/DEBUG:     #30  pc 00017bf9  <unknown>
    I/DEBUG:     #31  pc 00008c2a  <unknown>
    I/DEBUG:          997b969c  00000000  
    I/DEBUG:          997b96a0  00000000  
    I/DEBUG:          997b96a4  00000000  
    I/DEBUG:          997b96a8  00000000  
    I/DEBUG:          997b96ac  00000000  
    I/DEBUG:          997b96b0  00000000  
    I/DEBUG:          997b96b4  00000000  
    I/DEBUG:          997b96b8  00000000  
    I/DEBUG:          997b96bc  00000000  
    I/DEBUG:          997b96c0  00000000  
    I/DEBUG:          997b96c4  00000000  
    I/DEBUG:          997b96c8  00000000  
    I/DEBUG:          997b96cc  00000000  
    I/DEBUG:          997b96d0  00000000  
    I/DEBUG:          997b96d4  00000000  
    I/DEBUG:          997b96d8  00000000  
    I/DEBUG:     #00  997b96dc  9ad1cdd4  /system/lib/libwebcore.so (WebCore::RenderLayer::updateLayerPosition()+52)
    I/DEBUG:          997b96e0  b84902e4  [heap]
    I/DEBUG:          997b96e4  b8490090  [heap]
    I/DEBUG:          997b96e8  0000000f  
    I/DEBUG:          997b96ec  0000002f  
    I/DEBUG:          997b96f0  b8490090  [heap]
    I/DEBUG:          997b96f4  00000001  
    I/DEBUG:          997b96f8  9ad2204e  /system/lib/libwebcore.so (WebCore::RenderLayer::repaintIncludingNonCompositingDescendants(WebCore::RenderBoxModelObject*)+14)
    I/DEBUG:          997b96fc  b827f428  [heap]
    I/DEBUG:          997b9700  9ba8b488  /system/lib/libwebcore.so
    I/DEBUG:          997b9704  b8490090  [heap]
    I/DEBUG:          997b9708  997b9748  [stack:11949]
    I/DEBUG:          997b970c  9ad35d38  /system/lib/libwebcore.so (WebCore::RenderLayerCompositor::updateBacking(WebCore::RenderLayer*, WebCore::RenderLayerCompositor::CompositingChangeRepaint)+344)
    I/DEBUG:          997b9710  b8490014  [heap]
    I/DEBUG:          997b9714  b848fe3c  [heap]
    I/DEBUG:          997b9718  00000012  
    I/DEBUG:          ........  ........
    I/DEBUG:     #01  997b9750  b8490360  [heap]
    I/DEBUG:          997b9754  b8490090  [heap]
    I/DEBUG:          997b9758  00000000  
    I/DEBUG:          997b975c  997b98e0  [stack:11949]
    I/DEBUG:          997b9760  997ba84f  [stack:11949]
    I/DEBUG:          997b9764  b843a9e4  [heap]
    I/DEBUG:          997b9768  00000034  
    I/DEBUG:          997b976c  00000180  
    I/DEBUG:          997b9770  b848fe3c  [heap]
    I/DEBUG:          997b9774  b85257f8  [heap]
    I/DEBUG:          997b9778  9ace0300  /system/lib/libwebcore.so (WebCore::RenderBox::dirtyLineBoxes(bool)+80)
    I/DEBUG:          997b977c  43400000  
    I/DEBUG:          997b9780  b83fe51c  [heap]
    I/DEBUG:          997b9784  997b9788  [stack:11949]
    I/DEBUG:          997b9788  004a0000  
    I/DEBUG:          997b978c  b848feb8  [heap]
    I/DEBUG:          ........  ........
    I/DEBUG:     #02  997b9aa0  b804b028  [heap]
    I/DEBUG:          997b9aa4  b8490360  [heap]
    I/DEBUG:          997b9aa8  00000000  
    I/DEBUG:          997b9aac  997b9c30  [stack:11949]
    I/DEBUG:          997b9ab0  997ba84f  [stack:11949]
    I/DEBUG:          997b9ab4  b83fe51c  [heap]
    I/DEBUG:          997b9ab8  00000000  
    I/DEBUG:          997b9abc  00000001  
    I/DEBUG:          997b9ac0  b86fd038  [heap]
    I/DEBUG:          997b9ac4  b86fd368  [heap]
    I/DEBUG:          997b9ac8  997b9b48  [stack:11949]
    I/DEBUG:          997b9acc  9adac54a  /system/lib/libwebcore.so (WebCore::TransformState::move(int, int, WebCore::TransformState::TransformAccumulation)+58)
    I/DEBUG:          997b9ad0  b83fe678  [heap]
    I/DEBUG:          997b9ad4  b8294d4c  [heap]
    I/DEBUG:          997b9ad8  00580b05  
    I/DEBUG:          997b9adc  b83fe770  [heap]
    I/DEBUG:          ........  ........
    I/DEBUG:     (no map below)
    I/DEBUG:     (no map for address)
    I/DEBUG:     20c1e000-20c1f000 
    

    【讨论】:

    • 你应该得到积分@MH。 !!现在可以了~我需要几个小时来解决所有的 css 问题,谢谢
    • @johann:很高兴听到这确实为您解决了问题!找到罪魁祸首主要是一个反复试验的过程,所以我并不奇怪你很难追查到它。 :)
    【解决方案3】:

    尝试删除这些行:

    wv.clearCache(true);
    wv.clearHistory();
    
    wv.setDownloadListener(new DownloadListener() {
        @Override
            public void onDownloadStart(String url, String userAgent,
                                        String contentDisposition,      String mimetype, long contentLength) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setType(mimetype);
                intent.setData(Uri.parse(url));
                startActivity(intent);
            }
        });
    
    wv.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                MainActivity.this.setProgress(progress * 1000);
            }
            public boolean onConsoleMessage(ConsoleMessage cm) {
                Log.d("MyProject: WebView: ", cm.message() + " -- From line "
                    + cm.lineNumber() + " of "
                    + cm.sourceId() );
            return true;
        }
        });
    

    你的版本可能与chrome客户端有问题

    【讨论】:

    • 谢谢。我已经删除了这些行,但仍然崩溃
    • 尝试创建一个内容简单的页面并进行测试。它可以是页面内容中的内容。
    【解决方案4】:

    您使用的是定制版的 Android 吗?一个特定的模式?如果你这样做 - 问题总是有可能是那个 ROM 的结果。在某些情况下,WebView 在特定版本的 Android 或特定 ROM 中遇到问题。 一些链接: https://code.google.com/p/chromium/issues/detail?id=481420 http://googlechromereleases.blogspot.com.au/2015/04/android-webview-stable-update.html http://forum.xda-developers.com/verizon-htc-one-m8/help/android-webviewer-update-huge-turd-t3091192/post60333480#post60333480 Android System WebView crashes Android apps! "Tombstones are disabled on JB MR2+ user builds"

    检查您的 WebView 是否是最新的。如果是 - 卸载更新并检查问题是否仍然存在。如果它不是最新的 - 进行更新并检查问题是否仍然存在。很可能不会。

    此外 - 这个问题可能不会发生太多。在我使用过的大多数设备中 - WebView 可靠且稳定。

    祝你好运。

    【讨论】:

    • 感谢您的宝贵建议。我的 android (4.1) 版本是普通版本(由 fujitsu 提供)。默认浏览器也会发生崩溃,我无法更新它。正如您所提到的,崩溃只发生在少数设备上。也许我的js代码也需要增强。我会检查的。
    • 您可以登录到浏览器控制台并使用 webviewclient 界面接收消息。它可以帮助您查明导致问题的 js 代码行。
    • 谢谢。按照您的建议,我尝试使用 onConsoleMessage 方法进行调试,但从未调用过该方法。
    • 如果您没有收到控制台消息 - 您可以按照此处的说明从您的 PC(您可能已经知道这一点)调试您的内置浏览器 - developer.chrome.com/devtools/docs/remote-debugging
    • 谢谢。崩溃后的日志消息在我的消息中。
    猜你喜欢
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 2021-07-28
    • 1970-01-01
    相关资源
    最近更新 更多