检测web视图错误和显示信息视图、错误、信息、web

2023-09-07 08:44:54 作者:世态炎凉何必善良

我想显示错误信息时出现错误加载web视图页面(无连接)。这是我迄今为止,没有错误处理code:

I'd like to show an error message when there is an error loading a webview page (No connection). This is what I have so far, without the error handling code:

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/");
    }
}

我将如何做到这一点?

How would I do this?

推荐答案

您是最有路...只是实施onReceivedError和处理所需的错误。

You're most of the way there... Just implement onReceivedError and handle the errors that you want.

 
精彩推荐
图片推荐