android illegal exception when dialog is showing

The top level of your stacktrace is telling you what is wrong:

java.lang.IllegalArgumentException:$DecorView{21f9ba68 V.E..... R.....ID 0,0-1136,402} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(
at android.view.WindowManagerGlobal.removeView(
at android.view.WindowManagerImpl.removeViewImmediate(

You are calling dismiss on a dialog that is currently not being shown anymore.
As in: your Activity/Fragment is possibly already destroyed when you call dismiss (-> “not attached to window manager”).

[edit] One way to fix this is to check for activity.isFinishing() or fragment.isAdded()

Please dismiss as follows

if ((alertDialog != null) && alertDialog.isShowing())