简体   繁体   中英

How to resolve the white Screen of WebView?

I'm using the below code to populate my WebView with local html files, but it appears fine in some device but recently I noticed in some Device like Colors X114 , the WebView appears fine for one Second and then everything disappears and white blank screen appears.

The Code:

package com.nepalpolice.mnemonics;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;


/**
 * Created by Sagar on 2017/09/23. yo chai menupage ko Fragments ko lagi
 */

public class Homepage extends Fragment {

    WebView myWebView;
    private LinearLayout container;
    private Button nextButton, closeButton;
    private EditText findBox;


    public Homepage() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_homepage, container,  false);


        String url = getArguments().getString("url");


        myWebView=(WebView)rootView.findViewById(R.id.webview);
        myWebView.getSettings().setBuiltInZoomControls(true);

        myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        myWebView.getSettings().setLoadsImagesAutomatically(true);
        myWebView.getSettings().setJavaScriptEnabled(true);

        myWebView.getSettings().setBuiltInZoomControls(true);

        myWebView.setInitialScale(1);
        myWebView.getSettings().setLoadWithOverviewMode(true);
        myWebView.getSettings().setUseWideViewPort(true);

        WebSettings webSettings = myWebView.getSettings();

        myWebView.loadUrl(url);


        return rootView;


    }

    public static String changedHeaderHtml(String htmlText) {

        String head = "<head><meta name=\"viewport\" content=\"width=device-width, user-scalable=yes\" /></head>";

        String closedTag = "</body></html>";
        String changeFontHtml = head + htmlText + closedTag;
        return changeFontHtml;

    }
}

and this is how it appears or should appears

在此处输入图像描述

But how it appears in some Device.

在此处输入图像描述

and I'm passing the url as string as below

  args3.putString("url1", "file:///android_asset/b/dbpm.html");

You try this:

WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowContentAccess(true);
settings.setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(url);

Most likely you've faced the issue with View Layer Type. Try this code:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    webSettings.setMixedContentMode(0);
    webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
    webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

And try also to set hardware acceleration in Manifest:

android:hardwareAccelerated="true"

Please refer this link for more details.

See you have to enable JavaScriptEnabled in it and in url must have http:

wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl(url);

You can take basic information in these links:

I have a problem with my website. All application requests went to AJAX.

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH'])')
{//the result is returned to the application}

then I changed the request to

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{//does not return to the application}

The white screen is gone and the app is working.

I could fix it by overrinding WebViewClient class, just implement onPageFinished() doing a validation whereas the loading progress of the page has finished.

Sometimes it can occur that the content of the page is not fully loaded. Just reload it until you reach 100 percent.

while(view.getProgress() < 100) ()-> {view.reload()};

You should have to implement something like a timeout validation.

private class MyWebViewClient extends WebViewClient
{
   @Override
   public void onPageFinished(WebView view, String url)
   {
      try
      {
         while(view.getProgress() < 100)
         {
             view.reload();
         }
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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