Android:进度对话框在关闭时出现

Android: progress dialog raises on close

我制作了一个 Util class 来像这样打开和关闭进度对话框

Utils.java

public class Utils {

    private static Utils util;
    private Typeface typeface;
    private ProgressDialog progressDialog;

    public static Utils getInstance(){

        if(util == null){
            util = new Utils();
        }
        return util;
    }

    public void showProgressDialog(Activity activity){

        if(progressDialog != null && progressDialog.isShowing()){
            return;
        }

        if(progressDialog == null ) {
            progressDialog = new ProgressDialog(activity);
        }
        progressDialog.setMessage("Loading...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(false);
        progressDialog.show();
     }

public void closeProgressDialog(){
    try {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }catch(Exception e){
        e.printStackTrace();
    }
  }
}

然后我在加载数据前调用 showProgressDialog,在加载数据后调用 closeProgressDialog。像这样

活动代码

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_dashboard,container,false);
    activity = getActivity();
    ButterKnife.bind(this,view);

    init();
    return view;
}

  private void init() {

    Utils.getInstance().showProgressDialog(activity);
    getPatientsList();


   //in response i am calling closeProgressDialog();
  }

 public void onPatientListResponse(String response){
         Utils.getInstance().closeProgressDialog(); // raises exception here
  }

但它在 closeProgressDialog() 中引发异常;

java.lang.IllegalArgumentException: View=com.android.internal.policy.PhoneWindow$DecorView{f7fd12e V.E...... R......D 0,0-1024,232} not attached to window manager

您甚至还没有初始化进度条,您正在尝试检查它是否显示...

此处您尚未初始化进度条,但由于您的条件是 && (AND),编译器应该检查两者并发现 progressDialog 为空,因此没有 isShowing 方法...

if(progressDialog != null && progressDialog.isShowing()){
        return;
    }
--------------------------------------------------
public void showProgressDialog(Activity activity){
    if(progressDialog == null ) {
        progressDialog = new ProgressDialog(activity);
    }
    progressDialog.setMessage("Loading...");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setCancelable(false);
    progressDialog.show();
 }

一个建议是使用 Context 而不是 activity 作为进度条。

干杯!

试试这个,

无论如何,你要检查 progressDialog 是否已经显示,如果是,你只是返回控件。而不是关闭 progressDialog 并创建一个新实例。因为旧的 progressDialog 可能以不同的上下文开始。

public void showProgressDialog(Activity activity){

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

        if(progressDialog == null ) {
            progressDialog = new ProgressDialog(activity);
        }
        progressDialog.setMessage("Loading...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(false);

        if(!activity.isFinishing())
           progressDialog.show();
     }
 public void showProgressDialog(Activity activity){

        if(progressDialog != null){  // Ensure it's not null
          if(progressDialog.isShowing()) // if it's showing than return.
            return;
        }else{
          //It will create an instance of new object.
            progressDialog = new ProgressDialog(activity);
        }

        progressDialog.setMessage("Loading...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(false);
        progressDialog.show();
     }

我以前遇到过这个问题并使用下面的代码解决了。

private boolean isDialogViewAttachedToWindowManager() {
    if (dialog.getWindow() == null) return false;
    View decorView = progressDialog.getWindow().getDecorView();
    return decorView != null && decorView.getParent() != null;
}

或尝试以下解决方案。

if (pDialog instanceof Activity && !((Activity) mContext).isFinishing())
        pDialog.show();