我做了一個實用程序類來打開和關閉進度對話框這樣安卓進度對話框上提高近
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();
}
}
}
,然後我之前的數據加載後的數據和closeProgressDialog調用showProgressDialog已被加載。這樣
Acvitity代碼
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
非常感謝。事實上,在之前的活動中,我在調用新活動之前沒有調用closeProgressDialog()。因此,在調用showProgressDialog()的新活動中,它剛剛返回上一個對話框。所以對話以前的活動,我在新活動中呼籲closedialog.Thanks很多! –
現在我確保在離開活動或片段之前調用closedialog。 –
@Androidjack很高興!幫助:) – Raghavendra