android webview displaying blank page

♀尐吖头ヾ 提交于 2019-12-18 11:45:04

问题


I have an android application that I am developing using the emulator running on android 2.3.3 with an embedded WebView in a framelayout nested in a linearlayout (vertical). No matter what code I use it never actually does anything. I have Permissions on the android application set to INTERNET. Even when trying to load just the HTML code. I get a blank white page, no errors no nothing. Nothing in logcat as well.

WebView wview = (WebView)findViewById(R.id.webView1);
wview.getSettings().setJavaScriptEnabled(true);

I have tried all three of these to attempt to load anything into the webview:

wview.loadUrl("http://www.google.com");
wview.loadData("<HTML><BODY><H3>Test</H3></BODY></HTML>","text/html","utf-8");
wview.loadDataWithBaseURL(null, "<HTML><BODY><H3>Test</H3></BODY></HTML>","text/html","utf-8",null);

All three give me the same results. Nothing.

Any ideas?


回答1:


WebSettings settings = wbView.getSettings();
settings.setDomStorageEnabled(true);



回答2:


It might be a bit too late, but I wanted to share my experience with you, because I had the same problem and spent a lot of time on it.

Put your WebView in RelativeLayout and not in FrameLayout.

What I have tested: My WebView's onPageFinished() was called every time, but on the screen I got blank page.

From chrome://inspect WebView debugger I was able to "ping" my WebView by pressing inspect button, and after that WebView was refreshing immediately.

My thoughts - WebView isn't rendered correctly in FrameLayout, and RelativeLayout fixes the issue (even invalidate() and requestLayout() calls didn't help).




回答3:


I think if you edit some of your code then it will be ok.Look how i do it in my case below.Try for you in this way..
For displaying a web page

final WebView wbView = (WebView) findViewById(R.id.WebView);

WebSettings settings = wbView.getSettings();
settings.setJavaScriptEnabled(true); 
wbView.loadUrl("https://play.google.com/store/apps");
wbView.clearView();
wbView.measure(100, 100);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);

For showing HTML:
See this tutorial..

  • android-tutorial-html-file-in-webview



回答4:


instead of passing null, you should pass an empty String to it. ex: ""
So you should call it like this:
wview.loadDataWithBaseURL("", "<HTML><BODY><H3>Test</H3></BODY></HTML>","text/html","utf-8","");




回答5:


I gave +1 because these were both valid assistance to getting the webview to working properly, but it was not the solution to my issue.

What I didn't realize is I had a wrap_content on the include of my webview layout and this caused it to have a height of 0px. By setting this to fill_parent I resolved the issue. (Webview with white background and app with white background you cant really tell its not full height when it shows up as full height in the layout designer.)

Thanks for your help though!




回答6:


clearView is deprecated.

Use:

webView.loadUrl("about:blank")



回答7:


change wrap_content to fill_parent in your webview layout. it worked for me




回答8:


I think What you need is WebViewClient. After setting webviewclient call webview.loadUrl(_url); Somethin like this ....

private void loadUrlInWebView(String _URL){
    WebView myWebView = (WebView) findViewById(R.id.webviewer);     

    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setUseWideViewPort(true);
    webSettings.setLoadWithOverviewMode(true);

    myWebView.setWebViewClient(new MyWebViewClient());
    myWebView.loadUrl(_URL);
}

private class MyWebViewClient extends WebViewClient{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {         

            view.loadUrl(url);

        return true;
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
    }
}



回答9:


If it still Doesn't work then please try adding "http://" or "https://" before the web address and it should work.




回答10:


Try increasing the RAM for your emulated device. That solved the problem for me.

I found this out by looking at the logcat messages in Android Monitor (Android Studio). The messages below gave me the idea about what was wrong to increasing RAM :

W/art: Throwing OutOfMemoryError "Failed to allocate a 6635532 byte allocation with 1170528 free bytes and 1143KB until OOM"

W/JavaBrowserViewRendererHelper: Error allocating bitmap

E/chromium: [ERROR:java_browser_view_renderer_helper.cc(132)] Error unlocking java bitmap pixels.



回答11:


My webview was displaying a white blank square on top of it, sometimes totally blank. This only occurred when it was loading certain data.

Changing it to a RelativeLayout, and setting layout_height to match_parent worked for me.

<RelativeLayout xmlns:android=...
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/ll_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="3dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        .....

    </RelativeLayout>

    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/ll_title"
        android:background="@color/colorYellow" />


    <ProgressBar
        android:id="@+id/pb_spinning"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:visibility="gone" />

</RelativeLayout>



回答12:


You can use the following code, this will be helpful:

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.example.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}



回答13:


use ip address instead of web url then override webclient using following ssl error handling method

@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
    handler.proceed(); // Ignore SSL certificate errors
}



回答14:


In my case, it was due to the fact that my webview was attempting to load a page using HTTPS where the certificate was untrusted.

You can check if this is the case in your code using the following snippet:

@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
    handler.proceed();
}

Java

override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
    handler?.proceed()
}

Kotlin

Then, if it is the case, quickly remove the snippet and refer to the documentation to diagnose the problem further and potentially add the certificate to your keystore.



来源:https://stackoverflow.com/questions/14300664/android-webview-displaying-blank-page

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