我想在加載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會更好嗎?根據文檔,它被稱爲頁面上的每個組件。如何在整個頁面中檢查它? –