关于出现这个问题,一般是因为Activity已经在finish了,但是还在dialog.show(),或者dialog.dismiss().导致window manager无法管理dialog。解决办法如下:
/**
* 20210913 安全关闭对话框 .
* 避免报:not attached to window manager .
* @param dialog
*/
public static void closeDialogSafety(Activity activity,Dialog dialog)
{
try{
if(!activity.isFinishing()&&dialog!=null&&dialog.isShowing())
{
dialog.dismiss();
}
}catch (Exception e)
{
}
}
/**
* 20210914 安全打开对话框 .
* 避免报:not attached to window manager .
* @param dialog
*/
public static void showDialogSafety(Activity activity,Dialog dialog)
{
try{
if(!activity.isFinishing()&&dialog!=null&&!dialog.isShowing())
{
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
}catch (Exception e)
{
}
}