Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalArgumentException - dialog.dismiss

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)
like image 878
tiagosito Avatar asked Sep 22 '16 13:09

tiagosito


2 Answers

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);
 }
like image 120
Jojo Narte Avatar answered Sep 28 '22 11:09

Jojo Narte


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 ();
             }
like image 41
anddevmanu Avatar answered Sep 28 '22 11:09

anddevmanu