我已经制作了一个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,在数据加载完成后会关闭它。代码如下:
Activity 代码
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