简体   繁体   English

Android webview 崩溃“致命信号 5 (SIGTRAP)”

[英]Android webview crash "Fatal signal 5 (SIGTRAP)"

I have an app with a web view in which I load HTML content with JavaScript enabled.我有一个带有 web 视图的应用程序,我在其中加载了 HTML 内容并启用了 JavaScript。 The web view is inside a fragment. web 视图位于片段内。

This is how I initialize the web view inside the method onCreateView of the fragment:这就是我在片段的onCreateView方法中初始化 web 视图的方式:

WebView webview = (WebView) mainView.findViewById(R.id.webview);

WebSettings webSettings = webview.getSettings();

webSettings.setJavaScriptEnabled(true);
webSettings.setDisplayZoomControls(false);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDomStorageEnabled(true);
webSettings.setSupportMultipleWindows(true);

webview.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        /*
        * My code
        */
    }
});

webview.setWebChromeClient(new WebChromeClient() {
    @Override
    public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
        WebView.HitTestResult result = view.getHitTestResult();
        String data = result.getExtra();
        if (data != null) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(data));
            startActivity(browserIntent);
        }
        return false;
    }
});

webview.loadDataWithBaseURL(baseUrl, htmlData, "text/html", "utf-8", "");

In the web view, a map is loaded with JavaScript. On this map, we can click on elements and load photos.在web视图中,一个map加载了JavaScript。在这个map上,我们可以点击元素,加载照片。 When clicked, the photo is displayed in a popup (still inside the web view).单击时,照片将显示在弹出窗口中(仍在 web 视图内)。 When I click on the back button to go back to the map, the app crashes.当我单击后退按钮将 go 返回到 map 时,应用程序崩溃了。

应用程序崩溃的 GIF

Here is the error log:这是错误日志:

A/libc: Fatal signal 5 (SIGTRAP), code 1 in tid 949 (Chrome_InProcRe) [ 03-21 11:26:08.510 364: 364 W/ ] debuggerd: handling request: pid=32610 uid=10289 gid=10289 tid=949 A/libc:致命信号 5 (SIGTRAP),tid 949 中的代码 1 (Chrome_InProcRe) [ 03-21 11:26:08.510 364: 364 W/ ] debuggerd: 处理请求:pid=32610 uid=10289 gid=10289 tid= 949

I tested and got the crash on Android 7.1.1, 6.0.1, 5.0.2.我在 Android 7.1.1、6.0.1、5.0.2 上测试并崩溃。 Then I tried with Android 4.4.2 and the app didn't crash.然后我尝试使用 Android 4.4.2,应用程序没有崩溃。

When I click on the back button (as we can see on the GIF), it should go back to the previous state with the popup closed当我点击后退按钮时(正如我们在 GIF 上看到的那样),它应该 go 返回到之前的 state 并关闭弹出窗口

这应该在 Activity.java 的 onCreate 方法之前

    webView.setWebViewClient(new MyBrowser());

Try to override the back navigation functionality and close the popup yourself.尝试覆盖后退导航功能并自己关闭弹出窗口。

You don't need to handle all the stack navigation logic, just have a state when you are showing this popup.您不需要处理所有堆栈导航逻辑,只需在显示此弹出窗口时有一个状态即可。 Apply your own navigation logic(like manually closing the popup).应用您自己的导航逻辑(例如手动关闭弹出窗口)。

void onBackPressed(){
    if(isTheBuggyPopupIsOn){
        closeTheBuggyPopup();
    }
    else{
        super.onBackPressed();
    }
}
    webView.setWebViewClient(new MyBrowser());


public class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            if (url.equals(""YOUR URL)) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            } else {
                return false;
            }

        }

    }

Try this....尝试这个....

First add this首先添加这个

webView.setWebViewClient(new MyBrowser());

and then add this然后添加这个

public class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            if (url.equals(""YOUR URL)) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            } else {
                return false;
            }

        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM