取消progressDialog以返回另一个活动时出现NullPointerException

时间:2011-10-28 06:05:14

标签: java android progressdialog

有人知道这里发生了什么吗?有没有办法让用户取消progressDialog并返回活动A而不会有任何崩溃?

重现此次崩溃的步骤:

活动A 中,我启动了一个新的意图活动B tabhost,在progressDialog加载完成之前加载“class1.java”(带有progressDialog的Webview),我快速点击后退按钮返回活动A ,我遇到了崩溃

如果符合以下条件,应用不会崩溃:

  • 我等到webview加载完成并返回活动A

  • 我取消progressDialog并继续浏览网页浏览,直到页面加载完毕并返回活动A

第51行: progressDialog.incrementProgressBy(进度);

Logcat: http://pastebin.com/uBw4xcaY

java.lang.NullPointerException
at com.domain.package.ui.class1$class1_$1.onProgressChanged(class1.java:51)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:358)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)

这是我的代码:

[...]

public class class1 extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_stack);

}

public static class class1_ extends Fragment {

    /** The Fragment's UI **/
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.class_, container, false);

        WebView engine = (WebView) v.findViewById(R.id.web_engine);

        final ProgressDialog progressDialog;
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMessage("Loading...");
        progressDialog.setCancelable(true);
        progressDialog.setProgress(0);
        progressDialog.show();

        engine.setWebChromeClient(new WebChromeClient() {

            public void onProgressChanged(WebView view, int progress) {

                progressDialog.setProgress(0);
                getActivity().setProgress(progress * 1000);

                progressDialog.incrementProgressBy(progress);

                if (progress == 100 && progressDialog.isShowing())
                    progressDialog.dismiss();
            }

        });

            engine.getSettings().setBuiltInZoomControls(false);
            engine.getSettings().setUseWideViewPort(false);
            engine.getSettings().setSupportZoom(false);
            engine.loadUrl("file:///android_asset/index.html");


        Toast.makeText(getActivity(),
                "Hello this is a toast",
                Toast.LENGTH_LONG).show();

        return v;
    }
  }
}

2 个答案:

答案 0 :(得分:6)

尝试更改onChanged()方法,如下所示:

public void onProgressChanged(WebView view, int progress) {
  if( progressDialog.isShowing() ) {
    progressDialog.setProgress(0);
    getActivity().setProgress(progress * 1000);

    progressDialog.incrementProgressBy(progress);

    if ( progress == 100 )
      progressDialog.dismiss();
  }
}

然后将其添加到片段onPause()方法:

if( progressDialog != null && progressDialog.isShowing() )
  dialog.dismiss();

答案 1 :(得分:3)

当您取消Dialog并返回时,Dialog设置为null,但您的WebChromeClient回调仍在加载,当进度发生变化时,会设置进度Dialog,但Dialognull因此导致NullPointerException。将您的setWebChromeClient()修改为。

engine.setWebChromeClient(new WebChromeClient() {
      public void onProgressChanged(WebView view, int progress) {
            if(progressDialog != null)
                progressDialog.setProgress(0);
                getActivity().setProgress(progress * 1000);
                progressDialog.incrementProgressBy(progress);
                if (progress == 100 && progressDialog.isShowing())
                    progressDialog.dismiss();
            }
      }
});