我有一个带有ProgressDialog的WebView,所以我的问题是在加载网站后ProgressDialog没有结束。 ProgressDialog不会解雇。我使用此代码
public class MainActivity extends Activity {
private WebView webView;
ProgressDialog mProgress;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
mProgress = ProgressDialog.show(this, "Loading", "Bitte warten...");
mProgress.setMax(100);
webView.setWebViewClient(new WebViewClient() {
});
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
webView.loadUrl("http://google.de/");
}
public class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
if(mProgress.isShowing()) {
mProgress.hide();
}
}
}
希望有人能帮我解决这个问题
答案 0 :(得分:1)
删除此行:
webView.setWebViewClient(new WebViewClient() { });
并替换
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
与
webView.setWebViewClient(new myWebClient (){
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
答案 1 :(得分:0)
使用myWebClient
类对象覆盖onPageFinished
方法来设置WebViewClient:
webView.setWebViewClient(new myWebClient());
并删除webView.setWebViewClient
的第二次调用。