当 url 在内部更改时,Web 视图不会加载

Web View doesn't load when url is changed internally

我像平常一样使用 webview

WebView webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
webView.setWebViewClient(new WebViewClient() {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        view.loadUrl(url);
        return true;
    }

    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

        Toast.makeText(WebViewActivity.this, description, Toast.LENGTH_SHORT).show();
    }
});

现在,当我在 google 上搜索内容并打开列表中的任何 url 时,它不会加载。它在设备上仍然是一个白屏。如果我通过代码加载 url 就可以了! webview 是否有任何限制,或者我需要启用它的任何其他设置?谢谢

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
        view.loadUrl(url);
        return true;
    }

这不是我所说的正常方式,您应该 return false 并让 webview 处理它。 (不太确定为什么会出现您所描述的行为,但它可能有助于解决问题)

看我的解决方案

private void webViewResult() {

    String indexPage = "localhost:8080/libProject/all-book.jsp?classNo="+classtype+"&filename="+subjecttype";
    httpResultWebView = indexPage;
    webview();

}

void webview() {
    private WebView wv;
    WebView webView = (WebView) findViewById(R.id.webView);
    webView.loadUrl(httpResultWebView);
    WebSettings settings = webView.getSettings();
    settings.setBuiltInZoomControls(true);
    settings.getAllowContentAccess();
    settings.getJavaScriptEnabled();
    settings.setUseWideViewPort(true);
    settings.setLoadWithOverviewMode(true);
    webView.setWebViewClient(new WebViewClient());
    wv = webView;
    wv.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimetype,
                                    long contentLength) {
            String fileName = URLUtil.guessFileName(url, contentDisposition, mimetype);
            DownloadManager.Request request = new DownloadManager.Request(
                    Uri.parse(url));
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalPublicDir("/Library", fileName);
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);
        }
    });
}

反复试验对我有用! 只需要像这样更改它,

webView.setWebViewClient(new WebViewClient());

它如我所愿。感谢其他人的付出。