WebView.getUrl() 返回 null

发布于 2024-09-08 16:29:19 字数 1739 浏览 5 评论 0原文

我的 Webview 有问题。我正在编写自己的 WebChromeClient 类来重写 onProgressChanged 方法,因为我想在页面加载完成时显示页面,然后隐藏启动屏幕。因为我只是希望它发生在特定的 URL 上,所以我将实际的 WebView URL 与特定的字符串进行比较,但是有一个问题,当调用 webview.getUrl() 方法时,我得到一个空指针我的申请完成了。

这是代码:

private class MyWebChromeClient extends WebChromeClient {
    @Override
    public void onProgressChanged (WebView webview, int newProgress) {
        super.onProgressChanged(webview,newProgress);
        if(webview.equals(w1) && newProgress == 100 && webview.getUrl().startsWith("https://ssl.facebook.com/login.php")) { 
            webview.setVisibility(WebView.VISIBLE);
            ImageView imageview = (ImageView)findViewById(R.id.ivsplash);
            imageview.setVisibility(ImageView.GONE);
            ProgressBar progressbar = (ProgressBar)findViewById(R.id.pbsplash);
            progressbar.setVisibility(ProgressBar.GONE);
        }
    }
}

我这样做是为了避免 webview 需要三四秒来渲染页面,但它不起作用。我之前使用的代码是:

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView webview, String url) {
        super.shouldOverrideUrlLoading(webview, url);
        webview.loadUrl(url);
        return true;
    }
    
    @Override
    public void onPageFinished(WebView webview, String url) {
        if(url.startsWith("https://ssl.facebook.com/login.php")) {
            webview.setVisibility(WebView.VISIBLE);
            ImageView imageview = (ImageView)findViewById(R.id.ivsplash);
            imageview.setVisibility(ImageView.GONE);
            ProgressBar progressbar = (ProgressBar)findViewById(R.id.pbsplash);
            progressbar.setVisibility(ProgressBar.GONE);
        }
    }
}

I have an issue with Webview. I'm programming my own WebChromeClient class overriding onProgressChanged method because I want to show the page when it finishes loading and then, to hide the splash screen. As I just want it to happen with a specific URL, I compare the actual WebView URL with a specific string, but there is a problem, I get a null pointer when webview.getUrl() method is called and my application finishes.

This is the code:

private class MyWebChromeClient extends WebChromeClient {
    @Override
    public void onProgressChanged (WebView webview, int newProgress) {
        super.onProgressChanged(webview,newProgress);
        if(webview.equals(w1) && newProgress == 100 && webview.getUrl().startsWith("https://ssl.facebook.com/login.php")) { 
            webview.setVisibility(WebView.VISIBLE);
            ImageView imageview = (ImageView)findViewById(R.id.ivsplash);
            imageview.setVisibility(ImageView.GONE);
            ProgressBar progressbar = (ProgressBar)findViewById(R.id.pbsplash);
            progressbar.setVisibility(ProgressBar.GONE);
        }
    }
}

I do this for avoid the webview takes three or four seconds to render the page, but it doesn't work. The code I used before was:

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView webview, String url) {
        super.shouldOverrideUrlLoading(webview, url);
        webview.loadUrl(url);
        return true;
    }
    
    @Override
    public void onPageFinished(WebView webview, String url) {
        if(url.startsWith("https://ssl.facebook.com/login.php")) {
            webview.setVisibility(WebView.VISIBLE);
            ImageView imageview = (ImageView)findViewById(R.id.ivsplash);
            imageview.setVisibility(ImageView.GONE);
            ProgressBar progressbar = (ProgressBar)findViewById(R.id.pbsplash);
            progressbar.setVisibility(ProgressBar.GONE);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

源来凯始玺欢你 2024-09-15 16:29:19

不确定这是否可以通过 WebChromeClient 实现,但我可以使用

WebViewClient 并重载 onLoadResourceonReceivedError 来 做到这一点> 和 onPageFinished

这就是我所做的

onLoadResource     
 mwebview.setVisibility(View.GONE);


onReceivedError
mwebview.setVisibility(View.VISIBLE);

onPageFinished
mwebview.setVisibility(View.VISIBLE);

,最初在 XML 中,webview 设置为 GONE

android:visibility="gone"

我也将它用于 Facebook 并且工作正常。

Not sure if this is possible with WebChromeClient, but I was able to do this using

WebViewClient and overloading onLoadResource, onReceivedError and onPageFinished

This is what I've done

onLoadResource     
 mwebview.setVisibility(View.GONE);


onReceivedError
mwebview.setVisibility(View.VISIBLE);

onPageFinished
mwebview.setVisibility(View.VISIBLE);

and intially in XML the webview is set as GONE

android:visibility="gone"

I too use it for Facebook and works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文