使用进度对话框时强制关闭此活动的错误

时间:2012-10-03 15:09:16

标签: android webview progressdialog

这段代码中有什么问题说力关闭? 使用进度对话框时发生错误

ourBrow = (WebView) findViewById(R.id.wvBrowser);
ourBrow.setBackgroundColor(Color.parseColor("#000000"));
ourBrow.getSettings().setJavaScriptEnabled(true);
ourBrow.setWebViewClient(new WebViewClient() {
    ProgressDialog dialog;
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        dialog = ProgressDialog.show(Moile_gole.this, "",
                    "Loading. Please wait...", true);
        ourBrow.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        dialog.dismiss();
            // HIDE PROGRESS DIALOG LOADING IT HAS FINISHED
        }

    });

    ourBrow.loadUrl("http://www.google.com");

logcat的:

10-03 20:43:59.440: D/AndroidRuntime(304): Shutting down VM 10-03 20:43:59.440: W/dalvikvm(304):
threadid=1: thread exiting with uncaught exception (group=0x4001d800) 10-03 20:43:59.461: E/AndroidRuntime(304): FATAL EXCEPTION: main 
10-03 20:43:59.461: E/AndroidRuntime(304): java.lang.NullPointerException 
10-03 20:43:59.461: E/AndroidRuntime(304): at com.mobile.specification.Moile_Specification$2.onProgressChanged(Moile_Specifica‌​tion.java:68) 
10-03 20:43:59.461: E/AndroidRuntime(304): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:358)
10-03 20:43:59.461: E/AndroidRuntime(304): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-03 20:43:59.461: E/AndroidRuntime(304): at android.os.Looper.loop(Looper.java:123)
10-03 20:43:59.461: E/AndroidRuntime(304): at android.app.ActivityThread.main(ActivityThread.java:4627) 
10-03 20:43:59.461: E/AndroidRuntime(304): at java.lang.reflect.Method.invokeNative(Native Method) 
10-03 20:43:59.461: E/AndroidRuntime(304): at java.lang.reflect.Method.invoke(Method.java:521) 
10-03 20:43:59.461: E/AndroidRuntime(304): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
10-03 20:43:59.461: E/AndroidRuntime(304): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
10-03 20:43:59.461: E/AndroidRuntime(304): at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

强制关闭由于对话框导致NullPointerException错误。仅当单击WebView中的链接时才调用shouldOverrideUrlLoading()方法,并且当您尝试通过webview.loadUrl(url)加载网页时不调用此方法。所以这里你的对话框没有初始化并导致NullPointerException。

我建议您使用webchromeclient和ProgressBar(pb)来显示网页加载进度,如下所示。

wv.setWebChromeClient(new WebChromeClient(){

         public void onProgressChanged(WebView view, int progress) 
         {

             pb.setProgress(progress);
             if(progress==100)
                 pb.setVisibility(View.GONE);
          }


    });