Web View未加载API级别19(KITKAT)

时间:2016-04-19 11:47:18

标签: android webview android-4.4-kitkat

我在Android中使用webView并尝试在API级别大于或等于19的情况下检查Android。我的代码适用于大于19的API级别但是WebView没有在API级别中打开19.可以有人请检查这个。 这是代码:

    public boolean loadingFinished = true;//is loading finished
public boolean redirect = false;// redirect to page
 /**
 * {@inheritDoc}
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.web_view, container, false);
    mWebView = (WebView) rootView.findViewById(R.id.webView);
    //initializing views
    backButton = (BUtton) rootView.findViewById(R.id.web_view_back_button);
    titleTextView = (TextView) rootView.findViewById(R.id.web_view_textView);
    progressBar = (ProgressBar) rootView.findViewById(R.id.web_view_progress_bar);
    backButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getActivity().onBackPressed();
        }
    });
    mWebView.setVisibility(View.GONE);//Visible the view after full loaded
    mWebView.setInitialScale(1);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setLoadWithOverviewMode(true);
    mWebView.getSettings().setUseWideViewPort(true);
    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    mWebView.setScrollbarFadingEnabled(false);
    //check for network connection if not available show error msg!!!
    if (!NetworkService.getConnectivityInfo().isConnected(getContext())) {
        progressBar.setVisibility(View.GONE);
        Toast.makeText(getContext(), getContext().getString(R.string.no_network_msg), Toast.LENGTH_SHORT).show();
    } else {
        mWebView.loadUrl("https://www.google.co.in");
    }
    progressBar.setVisibility(View.VISIBLE);
    mWebView.setWebViewClient(new WebViewClient() {

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
            if (!loadingFinished) {
                redirect = true;
            }

            loadingFinished = false;
            view.loadUrl(urlNewString);
            return true;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            loadingFinished = false;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void onPageFinished(WebView view, String url) {
            if (!redirect) {
                loadingFinished = true;
            }

            if (loadingFinished && !redirect) {
                progressBar.setVisibility(View.GONE);
                //After page is loaded then whole web-view is visible
                mWebView.setVisibility(View.VISIBLE);
            } else {
                redirect = false;
            }

        }
    });

    return rootView;
}

2 个答案:

答案 0 :(得分:0)

API 19中有一个WebView引擎更改。 http://developer.android.com/guide/webapps/migrating.html

答案 1 :(得分:0)

在API级别19中,函数setInitialScale(int scaleInPercent)不起作用。说明代码在大于19的API级别上运行良好的原因。从我的代码中删除mWebView.setInitialScale(1);之后,它在API级别19中运行良好