我想在加载webview页面时发生错误消息(无连接)。这是我到目前为止,没有错误处理代码:检测Webview错误并显示消息
public class TrackerPage extends Activity {
// @Override
private WebView webview;
private ProgressDialog progressDialog;
private boolean error;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get rid of the android title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set the XML layout
setContentView(R.layout.tracker_page);
// Bundle objectbundle = this.getIntent().getExtras();
webview = (WebView) findViewById(R.id.tracker);
final Activity activity = this;
// Enable JavaScript and lets the browser go back
webview.getSettings().setJavaScriptEnabled(true);
webview.canGoBack();
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onLoadResource(WebView view, String url) {
// Check to see if there is a progress dialog
if (progressDialog == null) {
// If no progress dialog, make one and set message
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("Loading please wait...");
progressDialog.show();
// Hide the webview while loading
webview.setEnabled(false);
}
}
public void onPageFinished(WebView view, String url) {
// Page is done loading;
// hide the progress dialog and show the webview
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
webview.setEnabled(true);
}
}
});
// The URL that webview is loading
webview.loadUrl("http://url.org/");
}
}
我该怎么做?
如果您的项目的目标API级别<23,如何使用上述内容? – Michael
@Michael,还有另一种API级别1引入的不同参数的方法 - onReceivedError(WebView,int,String,String) – cdeange
如果没有互联网,我看到的只是“找不到网页”,并且没有任何回调叫做。怎么来的?另外,使用onReceivedError的新API会更好吗?根据文档,它被称为页面上的每个组件。如何在整个页面中检查它? –