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

发布于 2024-12-12 05:53:23 字数 3073 浏览 1 评论 0原文

有人知道这里发生了什么事吗?有没有什么方法可以让用户取消 ProgressDialog 并返回到 Activity A 而不会发生任何崩溃?

重现此崩溃的步骤:

活动A中,我启动一个新的意图活动B选项卡主机,加载“class1.java”(带有进度对话框)在进度对话框加载完成之前,我快速点击后退按钮返回到活动A,然后我崩溃了

应用程序不会崩溃,如果:

  • 我等到 webview 加载完成然后继续返回活动 A

  • 我取消 ProgressDialog 并停留在 Web 视图上,直到页面加载完成并返回活动 A

第 51 行: ProgressDialog.incrementProgressBy(progress);

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;
    }
  }
}

Anyone know what is going on at here? Is there any way to allow user to cancel the progressDialog and go back to Activity A without having any crash ?

Steps to reproduce this crash :

In an activity A and I start a new intent Activity B tabhost that load "class1.java" (Webview with progressDialog) before the progressDialog load finish , i quicky hit the back button to go back to Activity A and i got a crash

The apps won't crash if :

  • I wait till the webview load finish and go back to Activity A

  • I cancel the progressDialog and stay on the webview till the page load finish and go back to Activity A

Line 51 : progressDialog.incrementProgressBy(progress);

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)

Here's my code :

[...]

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;
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风念你 2024-12-19 05:53:23

尝试像这样更改您的 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();

Try changing your onChanged() method like this:

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();
  }
}

And then add this to the fragments onPause() method:

if( progressDialog != null && progressDialog.isShowing() )
  dialog.dismiss();
韬韬不绝 2024-12-19 05:53:23

当您取消 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();
            }
      }
});

When you cancel Dialog and go back, the Dialog is set to null but your WebChromeClient callback is still loading and when progress is changed, progress is set on Dialog, but the Dialog is null so cause NullPointerException. Modify your setWebChromeClient() as.

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();
            }
      }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文