WebView Rendering Issue in Android KitKat

倖福魔咒の 提交于 2019-11-26 12:39:01

问题


I\'ve been working on an application which have a WebView in which a static page get loaded from the assets (Also using JavaScript). This WebView is not working in KitKat, it remains blank. I am aware of the change in rendering engine (webkit to chromium) which happened in WebView in kitkat and tried the steps for migrating, which is given in Android Developers page. But it didn\'t help.

In logcat I am getting an error which is thrown from the Chromium source.

W/AwContents﹕ nativeOnDraw failed; clearing to background color.

Please suggest a workaround.


回答1:


In my case, in Android 4.4, I was getting a black background no matter I set what and this error message in my LogCat: nativeOnDraw failed; clearing to background color.

From Googling, it seems to be because hardware accelerated canvas rendering is not supported in Chromium WebView. I added this line to the WebView to turn off hardware accelerated canvas and now it works.

mWebview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);



回答2:


I ran into the same issue, but I did find a workaround. All you have to do is explicitly set a CSS background for your webpage. Like so:

body {
  background: white;
}

As it turns out if you do not explicitly set a background for a webpage the WebView will fail to draw said background and you'll end up with a transparent WebView.




回答3:


This seems to be a chromium webview bug.

Here is a thread about the issue: https://jira.appcelerator.org/browse/TIMOB-16479

Apparently, the accepted answer is not a sure fix. A workaround is mentioned in the link.




回答4:


The disabling of the hardware accelerator comes with heavy performance toll, In my case I found out that in Kitkat this happened to me when I was re instantiating the webview element within an activity that was finished and later restarted. After a lot of trial and error, when I added:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.webViewContainer);
layout.removeAllViews();
webview.destroy();

Just before ending the activity, it seems to have the problem solved. I haven't tested it on many devices yet but if this solution is proper, then it is better by far than disabling the hardware acceleration for KitKat for the webview.




回答5:


package com.example.testandroid;



public class MainActivity extends ActionBarActivity {

WebView webView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    if (savedInstanceState != null)
    {
        ((WebView)findViewById(R.id.web_view)).restoreState(savedInstanceState);
    }
    else{

        webView=(WebView)findViewById(R.id.web_view);
        webView.loadUrl("http://www.google.co.in");
        webView.getSettings().getJavaScriptEnabled();
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        webView.setWebViewClient(new WebViewClient()

        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view,
                    String url) {
                // TODO Auto-generated method stub
                view.loadUrl(url);
                return true;
            }
        });
    }
}


protected void onSaveInstanceState(Bundle outState) {
    webView.saveState(outState);
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
        webView.goBack();
        return true;
    }
    // If it wasn't the Back key or there's no web page history, bubble up to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}

}


来源:https://stackoverflow.com/questions/20675554/webview-rendering-issue-in-android-kitkat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!