做一个LoadingDialg工具类:
//Created by 方毅超 on 2017/7/19.
//数据加载框
public class LoadingDialogUtils {
/**
* 数据访问等待框
*/
private static ProgressDialog loadingDialog;
private static WeakReference<Activity> reference;
public static void init(Activity act) {
if (loadingDialog == null || reference == null || reference.get() == null || reference.get().isFinishing()) {
reference = new WeakReference<>(act);
loadingDialog = new ProgressDialog(act);
loadingDialog.setMessage("加载中...");
loadingDialog.setCancelable(false);
}
}
public static void setCancelable(boolean b) {
if (loadingDialog == null) return;
loadingDialog.setCancelable(b);
}
/**
* 显示等待框
*/
public static void showDialogLogin(Activity act) {
init(act);
loadingDialog.show();
}
/**
* 隐藏等待框
*/
public static void hideDialogLogin() {
if (loadingDialog != null) {
loadingDialog.dismiss();
}
}
/**
* 注销加载框,避免发生内存泄露
*/
public static void unInit() {
hideDialogLogin();
loadingDialog = null;
reference = null;
}
}
=============================================================
1.在调用的时候LoadingDialogUtils.showDialogLogin(getActivity());
虽然这里的参数是Context,但是不能使用getApplicationContext()或者getContext(),必须使用xxxActivity.this或者geeActivity(),因为只有Activity才能添加Window.
//解决办法,传入xxxActivity.this或者geeActivity()即可。
2.错误原因是Dialog在show的时候必须要有一个activity作为窗口载体,上面的日志的意思是承载Dialog的activity已经被销毁了,不存在了
解决办法
1、在show之前加判断activity是否被销毁了
if(!isFinishing()){
dialog.show();
}