WebView loadUrl 只工作一次 [英] WebView loadUrl works only once

查看:34
本文介绍了WebView loadUrl 只工作一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我多年前参与过这个项目,但不幸的是,我无法验证任何答案是否适用于给定的场景.

我很难使用一个 WebView 来显示我们的博客.初始化后,它工作得很好.用户可以导航到 WebView 中的不同链接.要返回博客,WebView 外有一个按钮,应再次加载主博客站点.

问题是,在第二次调用 loadUrl 后没有加载任何内容.这是我的代码:

私有WebView wv;@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.setContentView(R.layout.blog);wv = (WebView) findViewById(R.id.blog_webview);wv.getSettings().setJavaScriptEnabled(true);wv.setWebViewClient(new WebViewClient() {@覆盖public void onPageStarted(WebView view, String url, Bitmap favicon {MyLog.logDump("onPageStarted:" + url);}@覆盖public void onPageFinished(WebView view, String url) {MyLog.logDump("onPageFinished:" + url);}});wv.loadUrl(Constants.BLOG_URL);}

我的 OnClickListener 调用的函数如下:

 public void reLoadUrl() {wv.loadUrl(Constants.BLOG_URL);}

但是尽管 onPageFinished 和 onPageStarted 中的日志显示我的 wv.loadUrl 正在被调用并且它正在加载正确的 url,但 webview 本身的内容并没有改变.我尝试清除缓存、历史记录、视图,尝试不同的 WebSettings,尝试使用 webView.goBack() - 不是结果.这些想法也行不通:android 中奇怪的 webview goBack 问题

有时,reLoadUrl 会显示所需的结果 - 但一旦失败,它就无法再次工作.任何想法可能会发生什么?我确实尝试阅读 WebView 代码,但找不到任何可以帮助我的东西.

我唯一可以补充的是,我们正在使用一些严重依赖 webViews 的广告网络 - 我试图将它们关闭,但我没有删除这些库,所以我不确定它们不是罪魁祸首.

有什么想法吗??

解决方案

完全按照以下顺序使用.

 wv.clearCache(true);wv.clearView();wv.reload();wv.loadUrl("关于:空白");wv.loadData(Constants.BLOG_URL);

EDIT: I worked on this project years ago and unfortunately I cannot verify if any of the answers is working in the given scenario.

I am having hard time with one WebView which should show our blog. When initilized, it works just fine. The user can navigate to different links within the WebView. To get back to the blog, there is a button outside the WebView which should load the main blog site again.

The problem is, that nothing is loaded after the second call to loadUrl. Here is my code:

private WebView wv;

        @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

            this.setContentView(R.layout.blog);

    wv = (WebView) findViewById(R.id.blog_webview);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon {
            MyLog.logDump("onPageStarted: " + url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
                            MyLog.logDump("onPageFinished: " + url);
        }
    });

    wv.loadUrl(Constants.BLOG_URL);
}

The function called by my OnClickListener is the following:

    public void reLoadUrl() {
        wv.loadUrl(Constants.BLOG_URL);

}

But despite that the logs in onPageFinished and onPageStarted show that my wv.loadUrl is being invoked and that it's loading the correct url, the content in the webview itself doesn't change. I've tried clearing the cache, the history, the view, tried different WebSettings, tried to use webView.goBack() - not result. Also those ideas don't work: Strange webview goBack issue in android

Sometimes, the reLoadUrl shows the desired result - but once it fails it no longer can be made to work again. Any ideas what could be happening? I did try to read the WebView code but I couldn't find anything that could help me.

The only thing that I can add, is that we are using some ad networks which are heavily dependent on webViews - I tried to turn those down, but I didn't remove the libraries so I am not sure that they are not the culprit.

Any ideas??

解决方案

Use exact in below sequence.

   wv.clearCache(true);
   wv.clearView();
   wv.reload();
   wv.loadUrl("about:blank");
   wv.loadData(Constants.BLOG_URL);

这篇关于WebView loadUrl 只工作一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆