WebView导致应用程序重新启动 - 致命信号11(SIGSEGV)

10
我有一个用于显示网页的活动。在某些手机上(例如华为Ascend),该活动会以一种导致应用重启的方式崩溃,但不会显示通常用于报告错误的Android弹出窗口。(其他手机,例如Nexus则从未崩溃。)似乎总是在加载网页时崩溃,但不是在任何特定点,例如有时在shoulOverrideUrlLoading期间,有时在onPageStarted或onPageFinished期间。
我认为WebView引起了这个问题 -
08-29 21:08:22.577:A/libc(957):致命信号11(SIGSEGV)位于0x00000008处(code=1),线程957
但我无法弄清楚为什么。
以下是这个活动的代码:
public class WebDisplay extends Activity
{

  private String sentUrl = "";
  public WebView myWebView;
  public ProgressBar spinner;

  @SuppressLint("SetJavaScriptEnabled")
  @Override
  protected void onCreate (Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    Log.d("WEBVIEW", "onCreate");
    getWindow().setBackgroundDrawableResource(R.color.white);

    Bundle extras = getIntent().getExtras(); 
    if (null != extras)
    {
      sentUrl = extras.getString("displayURL");
    }

    setContentView(R.layout.webdisplay);

    myWebView = (WebView) findViewById(R.id.webDisplay);
    spinner = (ProgressBar) findViewById(R.id.webLoading);

    // WebView displays default to showing things in exact pixel size at 100%
    // resolution, meaning it generally opens on a zoomed in portion of the 
    // top left of the web page and refuses to let you zoom out, so we have
    // to force it to display pages a bit more reasonably.
    myWebView.getSettings().setLoadWithOverviewMode(true);
    myWebView.getSettings().setUseWideViewPort(true);
    myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);

    // By default, there is no javascript support or zoom controls, so
    // turn both of those on.
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setSupportZoom(true);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

    // We want links to continue opening inside the app.
    myWebView.setWebViewClient(new WebViewClient() {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
          Log.d("WEBVIEW", "loadUrl="+url);
          view.loadUrl(url); // open link in the same web view.          
          return true;
      }

      @Override
      public void onPageStarted(WebView view, String url, android.graphics.Bitmap favicon) {
        Log.d("WEBVIEW", "onPageStarted="+url);
        super.onPageStarted(view, url, favicon);
        if (View.GONE == spinner.getVisibility()) { spinner.setVisibility(View.VISIBLE); }
      }

      @Override
      public void onPageFinished(WebView view, String url) {
        Log.d("WEBVIEW", "onPageFinished="+url);
        super.onPageFinished(view, url);
        if (View.VISIBLE == spinner.getVisibility()) { spinner.setVisibility(View.GONE); }
      }

      @Override
      public void onReceivedError(WebView view, int errorCode, String description, String url) {
        Log.d("WEBVIEW", "onPageError="+url+"::descr="+description);
      }

    });

    // Try to get YouTube videos working.
    myWebView.setWebChromeClient(new WebChromeClient() {
    });

    // So the user doesn't have to back through their entire history to get 
    // back to the app, we provide a "done browsing" button.
    Button doneButton = (Button) findViewById(R.id.doneButton);
    doneButton.setOnClickListener(new OnClickListener(){
      @Override
      public void onClick(View v)
      {
        finish();
      }
    });

    ImageButton backButton = (ImageButton) findViewById(R.id.webBackButton);
    backButton.setOnClickListener(new OnClickListener(){
      @Override
      public void onClick(View arg0)
      {
        myWebView.goBack();
      }      
    });

    ImageButton fwdButton = (ImageButton) findViewById(R.id.webForwardButton);
    fwdButton.setOnClickListener(new OnClickListener(){
      @Override
      public void onClick(View arg0)
      {
        myWebView.goForward();
      }      
    });

    ImageButton refreshButton = (ImageButton) findViewById(R.id.refreshButton);
    refreshButton.setOnClickListener(new OnClickListener(){
      @Override
      public void onClick(View arg0)
      {
        myWebView.reload();
      }      
    });

    myWebView.loadUrl(sentUrl);
  }

  @Override
  protected void onSaveInstanceState(Bundle outState)
  {
    super.onSaveInstanceState(outState);

    // Save the state of the WebView
    myWebView.saveState(outState);
  }

  @Override
  protected void onRestoreInstanceState(Bundle savedInstanceState)
  {
    super.onRestoreInstanceState(savedInstanceState);

    // Restore the state of the WebView
    myWebView.restoreState(savedInstanceState);
  }
}

这是已加载的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white" >


   <RelativeLayout
        android:id="@+id/webNavBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/abs__background_holo_light"
        android:layout_alignParentBottom="true"
        android:padding="2dp" >

    <Button
        android:id="@+id/doneButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/refreshButton"
        android:layout_alignTop="@+id/refreshButton"
        android:background="@drawable/custom_button"
        android:padding="5dp"
        android:text="@string/web_done"
        android:textColor="@color/white"
        android:textStyle="bold" />

    <ImageButton
        android:id="@+id/refreshButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/custom_button"
        android:src="@drawable/ic_action_refresh"
        android:contentDescription="@string/web_refresh"
        android:textColor="@color/white" 
        android:padding="5dp" />

     <ImageButton
         android:id="@+id/webBackButton"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentTop="true"
         android:layout_marginRight="5dp"
         android:layout_toLeftOf="@+id/webForwardButton"
         android:background="@drawable/custom_button"
         android:src="@drawable/navigation_back"
         android:padding="5dp"
         android:contentDescription="@string/web_back"
         android:textColor="@color/white" />

    <ImageButton
        android:id="@+id/webForwardButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/custom_button"
        android:src="@drawable/navigation_forward"
        android:contentDescription="@string/web_forward"
        android:textColor="@color/white" 
        android:padding="5dp" />



     </RelativeLayout>

     <WebView
        android:id="@+id/webDisplay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_above="@id/webNavBar" />

     <ProgressBar
         android:id="@+id/webLoading"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentTop="true"
         android:layout_marginTop="80dp"
         style="@android:style/Widget.ProgressBar.Large"         
         android:layout_centerHorizontal="true" />

</RelativeLayout>

预计完成时间:像这样关闭硬件加速

myWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

似乎可以解决SIGSEV问题,但也会导致YouTube视频无法播放(我仍然能听到声音,但看不到图像)。


你看过这些吗?https://dev59.com/mmYr5IYBdhLWcg3wAFrEhttps://dev59.com/eWgv5IYBdhLWcg3wW_h_ - Jason Crosby
3
@JasonCrosby - 是的,遗憾的是,“这是设备固件中的一个漏洞”这个答案似乎是最有可能正确的(同时在实际防止错误方面最没有帮助;该死的,Android 设备制造商!) - Ben Williams
哈哈,没错,当有个bug出现而你却束手无策时,总是让人沮丧啊。 - Jason Crosby
嗨,我也在我的设备上遇到了类似的问题。当我打开一个https网址后几分钟,主活动就重新启动了。没有任何错误显示出来。你解决了吗?怎么解决的?请帮忙。 - Rat-a-tat-a-tat Ratatouille
1
三星Galaxy Tab上也有同样的问题 :( - cprcrack
2个回答

1
确保WebView是RelativeLayout的第一个元素。 或者不使用RelativeLayout,尝试LinearLayout。 或者删除WebView的以下属性: android:layout_alignParentTop="true" android:layout_above="@id/webNavBar"

8
这是什么原因? - Jaec

0
webSettings.setJavaScriptEnabled(true);

上述设置会导致某些使用JavaScript的网站出现"Fatal signal 11 (SIGSEGV)"错误。我在三星Galaxy Tab 3和Galaxy tab 3 lite上重现了这个错误。这两个设备的Android版本均为4.4.2。

如果我删除"setJavaScriptEnabled"函数,它就可以正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接