WebView.getUrl()返回null [英] WebView.getUrl() returns null
问题描述
我有一个网页视图的问题。我编程我自己WebChromeClient类中重写onProgressChanged方法,因为我想说明的页面,当它完成加载,然后隐藏闪屏。正如我只是希望它有一个特定的URL发生,我比较特定字符串的实际的WebView网址,但有一个问题,我得到一个空指针时webview.getUrl()方法被调用,我的应用程序完成。
这是code:
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);
}
}
}
我这样做对避免的WebView需要三四秒来渲染页面,但它不工作。在code我之前所用的是:
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);
}
}
}
感谢
推荐答案
不知道这是可能的 WebChromeClient
,但我可以做到这一点使用
Not sure if this is possible with WebChromeClient
, but I was able to do this using
WebViewClient
和重载 onLoadResource
, onReceivedError
和 onPageFinished
这是我做了什么
onLoadResource
mwebview.setVisibility(View.GONE);
onReceivedError
mwebview.setVisibility(View.VISIBLE);
onPageFinished
mwebview.setVisibility(View.VISIBLE);
和intially在XML web视图设置为GONE
and intially in XML the webview is set as GONE
android:visibility="gone"
我也用它来Facebook和正常工作。
I too use it for Facebook and works fine.
这篇关于WebView.getUrl()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!