0

我有以下 webview 场景

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view_tutorial);
        mWebView = (WebView) findViewById(R.id.webView1);

        mWebView.setWebViewClient(mWebViewClient);

        mWebView.setInitialScale(0);
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.requestFocusFromTouch();

        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);

        Button btnGo = (Button) findViewById(R.id.button1);
        btnGo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String url = "http://www.google.com";// here goes my server url with https://...... for authentication
                showProgressDialog("Loading");
                mWebView.loadUrl(url);
            }
        });


}    
private void showProgressDialog(String title) {

        if(mProgress == null || !mProgress.isShowing()){
            mProgress = ProgressDialog.show(this, title, "Please wait...", true,
                    true, new OnCancelListener() {
                public void onCancel(DialogInterface pd) {
                    finishActivity();
                }
            });
            mProgress.setCanceledOnTouchOutside(false);
            mProgress.setCancelable(true);  
        }       
    }
private void finishActivity() {
    if(mWebView!=null){
        finish();   
    }

}
private final WebViewClient mWebViewClient = new WebViewClient() {



        public void onPageStarted(WebView view, String url,
                android.graphics.Bitmap favicon) {

            super.onPageStarted(view, url, favicon);
            Log.d("test", "page started");

        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.d("test", "page should override called");
                return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {

            if (mIsLoadingSigninPage) {
                    mIsLoadingSigninPage = false;
                dismissProgressDialog();
            }
            Log.d("test", "page finished");
            super.onPageFinished(view, url);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {

            mIsLoadingSigninPage = false;
            dismissProgressDialog();
            Log.d("test", "page error received");


            super.onReceivedError(view, errorCode, description, failingUrl);
            finishActivity();
        }
};
private void dismissProgressDialog() {
    if (mProgress != null && mProgress.isShowing()) {
        mProgress.dismiss();
        mProgress =null;
    }
}

// 这是下面第 3 点的日志猫

 03-07 07:31:20.977: D/test(1431): page started
03-07 07:31:22.788: D/test(1431): page should override called
03-07 07:31:22.887: D/test(1431): page started
03-07 07:31:24.496: D/test(1431): page finished

好的,

  1. 当网络不可用时,我运行此代码,调用 webviewclient 的 onReceivedError,
  2. 如果网络可用并且页面加载开始,并且网络断开连接,那么也会调用 onReceivedError。
  3. 但是有一些时间实例,(按下按钮并在模拟器上使用 f8 断开网络后计数 1、2、3) mWebView.loadUrl(url) 被调用,webViewclient 的方法被调用如下

并且 WebView 显示空白屏幕,未调用 onReceivedError。这是logcat

  1. 这段代码有什么问题。
  2. 为什么没有调用 onReceivedError。如果它没有被调用,如何处理这种情况
  3. 如何知道 webView 没有加载任何内容并完成活动
4

1 回答 1

0

它肯定会调用页面启动和页面完成,因为 onReceivedError 将被调用:-

-单击任何链接(如果不存在互联网)然后应该覆盖 url 将抛出一个错误,它将处理
-on 活动开始或调用 webview

对于 onRecievedError 未处理的所有异常,您必须检查 http 状态

于 2013-03-07T11:44:06.727 回答