进度条有时会给出空指针

时间:2014-08-28 08:24:14

标签: android android-progressbar

我已经制作了一个方法来显示进度条。它工作正常,但有时我的应用程序关闭。我不知道为什么会发生这种情况。

代码

public static void showProgress(Context context, String msg, boolean isVisible) {


        if (progressDialog == null) {
            progressDialog = new ProgressDialog(context);
            progressDialog.setMessage(msg);
            progressDialog.setCancelable(false);
        }

        if (isVisible) {
            progressDialog.show();
        } else if (isVisible == false) {
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }

    }

logcat的

08-28 16:15:51.665    8477-8477/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@42186fd0 is not valid; is your activity running?
            at android.view.ViewRootImpl.setView(ViewRootImpl.java:664)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.app.Dialog.show(Dialog.java:281)
            at example.com.pocketdocs.CommonFunctions.CommonFunctions.showProgress(CommonFunctions.java:111)
            at example.com.pocketdocs.Group.ContactListFragment$contactSyncAsync.onPreExecute(ContactListFragment.java:163)
            at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
            at android.os.AsyncTask.execute(AsyncTask.java:534)
            at example.com.pocketdocs.Group.ContactListFragment.onActivityCreated(ContactListFragment.java:69)
            at android.app.Fragment.performActivityCreated(Fragment.java:1703)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
            at android.app.BackStackRecord.run(BackStackRecord.java:694)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
            at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5370)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

我写的代码是正确的还是我必须进行更改?????

2 个答案:

答案 0 :(得分:1)

提出这个条件,它应该可行

if(!((Activity) context).isFinishing())
{
          if (progressDialog == null) {
        progressDialog = new ProgressDialog(context);
        progressDialog.setMessage(msg);
        progressDialog.setCancelable(false);
    }

    if (isVisible) {
        progressDialog.show();
    } else if (isVisible == false) {
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }
}

答案 1 :(得分:0)

您需要Activity作为Context才能显示对话框。如果您尝试使用非活动上下文(例如Application

),则会出现此异常