java.lang.IllegalArgumentException - dialog.dismiss
Asked Answered
H

4

7

I am getting this error in my published application, only clients receive this error. I already tried several times to replicate the same mistake however unsuccessfully. I also already tried to use the below code at all locations where there is a Dialog but also not solved.

if (dialog.isShowing ()) {
    dialog.dismiss ();
}

The error report

java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{16faa139 V.E..... R.....I. 0,0-0,0} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:412)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:338)
at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:122)
at android.app.Dialog.dismissDialog(Dialog.java:522)
at android.app.Dialog.dismiss(Dialog.java:504)

**at br.my.project.de.a(Unknown Source)
at br.my.project.de.onPostExecute(Unknown Source)**

at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6946)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Hershel answered 22/9, 2016 at 13:15 Comment(4)
Is dialog in activity or fragment ? When you dismiss dialog check if it is null or notNapier
Hi @Napier the dialog is in ActivityHershel
in onDestroy() method of activity just check if it is not null then cancel the dialog and make its object null.Napier
@Napier Okay, I'll try it! but it has some way to replicate this error in development mode?Hershel
F
5

I could see that you are trying to dismiss a ProgressDialog on the postExecute of an AsyncTask. This in itself is a good practice but is sometimes buggy, I kind of experienced this also before especially while you're showing the ProgressDialog and suddenly rotate the view.

A solution I've found to fix this is below:

You will need these function to handle the proper dismissal and avoid crashes.

private void dismissProgressDialog(ProgressDialog progressDialog) {
            if (progressDialog != null) {
                if (progressDialog.isShowing()) {

                    //get the Context object that was used to create the dialog
                    Context context = ((ContextWrapper) progressDialog.getContext()).getBaseContext();

                    // if the Context used here was an activity AND it hasn't been finished or destroyed
                    // then dismiss it
                    if (context instanceof Activity) {

                        // Api >=17
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                            if (!((Activity) context).isFinishing() && !((Activity) context).isDestroyed()) {
                                dismissWithExceptionHandling(progressDialog);
                            }
                        } else {

                            // Api < 17. Unfortunately cannot check for isDestroyed()
                            if (!((Activity) context).isFinishing()) {
                                dismissWithExceptionHandling(progressDialog);
                            }
                        }
                    } else
                        // if the Context used wasn't an Activity, then dismiss it too
                        dismissWithExceptionHandling(progressDialog);
                }
                progressDialog = null;
            }
        }


public void dismissWithExceptionHandling(ProgressDialog dialog) {
            try {
                dialog.dismiss();
            } catch (final IllegalArgumentException e) {
                // Do nothing.
            } catch (final Exception e) {
                // Do nothing.
            } finally {
                dialog = null;
            }
        }

On your AsyncTask's onPostExecute implement the function.

@Override
 protected void onPostExecute(Boolean b) {
     // pass in the progressDialog as a parameter to the method
     dismissProgressDialog(progressDialog);
 }
Feature answered 12/1, 2018 at 5:39 Comment(1)
This seems to be a link to an answer that you had found: https://mcmap.net/q/150517/-java-lang-illegalargumentexception-view-not-attached-to-window-managerShebeen
R
2
fun Activity?.dismissDialog(dialog: Dialog?) {
    if (isActivityActive()) {
        dialog?.apply {
            if (isShowing) dismiss()
        }
    }
}


fun Activity?.isActivityActive(): Boolean {
    return null != this && !isFinishing && !isDestroyed
}
Regenaregency answered 25/5, 2017 at 19:14 Comment(0)
F
1

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.

Ferde answered 22/9, 2016 at 13:24 Comment(0)
N
1

Write this code in your activity's

onStop()

method.When anybody presses the home button and if dialog is opened than this error will come. Because on click of home button onPause() and onStop() method calls.Hope this helps.

 if (dialog!=null && dialog.isShowing ()) {
          dialog.dismiss ();
             }
Negress answered 22/9, 2016 at 13:50 Comment(1)
Not working. I've already called dismiss in the onPause.Sclerenchyma

© 2022 - 2024 — McMap. All rights reserved.