0

这段代码中有什么问题,它说强制关闭?使用进度对话框时出错

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

日志猫:

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)
4

1 回答 1

1

由于对话框引起的 NullPointerException,发生强制关闭错误。shouldOverrideUrlLoading() 方法仅在单击 WebView 内的链接时调用,而不是在您尝试通过 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);
          }


    });
于 2012-10-03T15:40:36.200 回答