Android:进度对话框关闭

时间:2016-11-14 09:30:31

标签: android performance progressdialog

我已经创建了一个Util类来打开和关闭像这样的

进度对话框

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

4 个答案:

答案 0 :(得分:2)

试试这个,

无论如何你要检查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();
     }

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

此处您还没有初始化您的进度条,但由于您的条件是&& (AND),编译器应检查两者并发现progressDialog为null,因此没有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();
 }

一个建议是使用上下文而不是进度条的活动。

干杯!

答案 3 :(得分:0)

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

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