这两天在做编辑记录的时候遇到的问题。
点击进入记录列表 RecordActivity,点击某条记录跳转到 HomeActivity 并进行编辑
ActivityCollector.finishAll(); HomeActivity.actionStart(RecordActivity.this,Integer.valueOf(info.id));
在 HomeActivity 中编辑完成后,点击提交,执行 showProgressDialog(this,"") 的时候报错
Unable to add window -- token android.os.BinderProxy@a88700d is not valid; is your activity running?
原因就是 dialog 依附的 Activity 不存在,所以报错闪退了。
showProgressDialog() 这个方法存在 BaseActivity 中,RecordActivity和HomeActivity 都继承自这个 BaseActivity
因为我有一个活动管理器,编辑跳转的时候要关闭所有的 Activity,原因就不说了,所以就造成了这个问题
解决办法,在参考了这篇博客后解决了
http://blog.csdn.net/u013673799/article/details/72084846
下面贴一下我的代码,在 BaseActivity 中
private static ProgressDialog progressDialog; private static WeakReference<Activity> mWeakReference; //显示dialog public static void showProgressDialog(Activity activity, String msg) { if(!isLiving(activity)){ return; } if(mWeakReference == null){ mWeakReference = new WeakReference(activity); } activity = mWeakReference.get(); if (progressDialog == null) { if (activity.getParent() != null) { progressDialog = new ProgressDialog(activity.getParent()); } else { progressDialog = new ProgressDialog(activity); } } if("".equals(msg)){ msg = "正在请求..."; } progressDialog.setMessage(msg); if (!progressDialog.isShowing()) { progressDialog.dismiss(); //点击屏幕其它地方是否会消失 progressDialog.setCanceledOnTouchOutside(false); progressDialog.show(); } } /** * 判断进度框是否正在显示 */ private static boolean isShowing(ProgressDialog dialog) { boolean isShowing = dialog != null && dialog.isShowing(); LogUtil.d(TAG,">------isShow:"+isShowing); return isShowing; } //关闭对话框 public static void closeProgressDialog() { if (isShowing(progressDialog) && isExistLiving(mWeakReference)) { progressDialog.dismiss(); progressDialog = null; mWeakReference.clear(); mWeakReference = null; } } private static boolean isExistLiving(WeakReference<Activity> weakReference) { if(weakReference != null){ Activity activity = weakReference.get(); if (activity == null) { return false; } if (activity.isFinishing()) { return false; } return true; } return false; } //判断Activity是否存活 private static boolean isLiving(Activity activity) { if (activity == null) { LogUtil.d(TAG, "activity == null"); return false; } if (activity.isFinishing()) { LogUtil.d(TAG, "activity is finishing"); return false; } return true; }
在请求http前执行 showProgressDialog(HomeActivity.this,"");
在结束请求后执行 closeProgressDialog();