WebView.getUrl() 返回 null
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定这是否可以通过
WebChromeClient
实现,但我可以使用WebViewClient
并重载onLoadResource
、onReceivedError
来 做到这一点> 和onPageFinished
这就是我所做的
,最初在 XML 中,webview 设置为 GONE
我也将它用于 Facebook 并且工作正常。
Not sure if this is possible with
WebChromeClient
, but I was able to do this usingWebViewClient
and overloadingonLoadResource
,onReceivedError
andonPageFinished
This is what I've done
and intially in XML the webview is set as GONE
I too use it for Facebook and works fine.