37
inviteBu.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(ChoiceList.size()>0)
             {

                    LayoutInflater factory = LayoutInflater.from(MobileConnectActivity.this);
                    final View textEntryView = factory.inflate(R.layout.invite_dialog, null);
                    final EditText et =(EditText) textEntryView.findViewById(R.id.usercontent_edit);

                   dia= new AlertDialog.Builder(MobileConnectActivity.this)
                        .setTitle(getString(R.string.invite_input_content))
                        .setView(textEntryView)
                        .setPositiveButton(getString(R.string.invite_send), new DialogInterface.OnClickListener() {
                            public void onClick(final DialogInterface dialog, int whichButton) {
                                dialog.dismiss();
                                 if(et.getText().toString()==null && et.getText().equals("") )
                                {
                                    Toast.makeText(getApplicationContext(), getString(R.string.invite_content_check), Toast.LENGTH_SHORT).show();
                                }
                                 else{


                                     new AsyncTask<Void, Void, String>() {

                                         CustomDialog mProgressBar = new CustomDialog(MobileConnectActivity.this, R.style.dialog);

                                        protected void onPreExecute() {

                                            mProgressBar.show();
                                        };

                                        protected void onCancelled() {
                                            mProgressBar.hide();
                                        };

                                        @Override
                                        protected String doInBackground(Void... params) {

                                                ChoiceList=removeDuplicateList(ChoiceList);
                                                for(int i=0;i<ChoiceList.size();i++)
                                                {
                                                    Log.i("aaa",""+ChoiceList.get(i));
                                                    sendSMS(ChoiceList.get(i), et.getText().toString());
                                                }
                                            return "OK";
                                        }

                                        protected void onPostExecute(String response) {
                                            mProgressBar.hide();

                                            if (response != null ) {

                                                 Toast.makeText(getApplicationContext(), getString(R.string.invite_succeed), Toast.LENGTH_SHORT).show();
                                                 Intent intent = new Intent();
                                                    intent.setClass(MobileConnectActivity.this, inviteMainActivity.class);
                                                    startActivity(intent);

                                                MobileConnectActivity.this.finish();
                                            } else {
                                                //mHelper.showResponseErrorMessage(response);
                                                Intent intent = new Intent();
                                                intent.setClass(MobileConnectActivity.this, inviteMainActivity.class);
                                                startActivity(intent);
                                                finish();
                                            }

                                        };

                                    }.execute();

                                 }

                            }
                        })
                        .setNegativeButton(getString(R.string.invite_cancel), new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {

                                /* User clicked cancel so do some stuff */
                            }
                        }).show();

             }
            else
            {
                Toast.makeText(getApplicationContext(), getString(R.string.invite_choice_check), Toast.LENGTH_SHORT).show();
            }
        }
    });

it give me :

07-21 03:36:24.519: E/WindowManager(23240): Activity com.portaura.myaura.invite.MobileConnectActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405c32d0 that was originally added here
07-21 03:36:24.519: E/WindowManager(23240): android.view.WindowLeaked: Activity com.portaura.myaura.invite.MobileConnectActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405c32d0 that was originally added here
07-21 03:36:24.519: E/WindowManager(23240):     at android.view.ViewRoot.<init>(ViewRoot.java:266)
07-21 03:36:24.519: E/WindowManager(23240):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:174)
07-21 03:36:24.519: E/WindowManager(23240):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:117)
07-21 03:36:24.519: E/WindowManager(23240):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
07-21 03:36:24.519: E/WindowManager(23240):     at android.app.Dialog.show(Dialog.java:241)
07-21 03:36:24.519: E/WindowManager(23240):     at com.portaura.myaura.invite.MobileConnectActivity$3$1$1.onPreExecute(MobileConnectActivity.java:161)
07-21 03:36:24.519: E/WindowManager(23240):     at android.os.AsyncTask.execute(AsyncTask.java:391)
07-21 03:36:24.519: E/WindowManager(23240):     at com.portaura.myaura.invite.MobileConnectActivity$3$1.onClick(MobileConnectActivity.java:201)
07-21 03:36:24.519: E/WindowManager(23240):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:165)
07-21 03:36:24.519: E/WindowManager(23240):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 03:36:24.519: E/WindowManager(23240):     at android.os.Looper.loop(Looper.java:130)
07-21 03:36:24.519: E/WindowManager(23240):     at java.lang.reflect.Method.invokeNative(Native Method)
07-21 03:36:24.519: E/WindowManager(23240):     at java.lang.reflect.Method.invoke(Method.java:507)
Rajesh
  • 15,724
  • 7
  • 46
  • 95
pengwang
  • 19,536
  • 34
  • 119
  • 168
  • Possible duplicate of [Activity has leaked window that was originally added](https://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added) – Leo supports Monica Cellio May 08 '18 at 15:45

9 Answers9

99

Window leaked exceptions are usually caused by dialogs which are not dismissed properly. ie if you are planning to dismiss a dialog in Onpostexecute of asynctask and the activity that created it has ended it will throw a window leak. Make sure you dimisss dialog in onPause of the activity.

Ajith M A
  • 3,838
  • 3
  • 32
  • 55
  • 1
    @pengwang Is this not the answer? – samus May 05 '14 at 16:11
  • 2
    For others with this error: Also make sure to look for errors above this error. An app crashing while the dialog is open will cause this error after the actual error that crashed the app. – Sub 6 Resources Sep 18 '17 at 19:50
  • Do these exceptions cause an app to crash? – IgorGanapolsky Aug 13 '18 at 15:55
  • So does the dialog stay open forever if I never dismiss it? I have a similar error with a "loading" dialog, but if I dismiss the dialog then it never actually appears on the screen. – Matt Payne Mar 24 '21 at 13:01
15

Whenever you initiate a ProgressDialog, that should be dismissed properly after the background task gets done, or even cancellation of the background running task. So,

instead of mProgressBar.hide(); use mProgressBar.dismiss();

you will not get android.view.WindowLeaked error

hope this helps

ABI
  • 1,536
  • 18
  • 38
11

Check if you are using finish() function before the mDialog.show() function. If it is remove the finish() and add it after the show().

Rana Ranvijay Singh
  • 6,055
  • 3
  • 38
  • 54
8

whren you miss this code : mProgressDialog.dismis(); that time may be you will receive this type of error.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
6

In onStop() or onDestroy() callback, dismiss the dialog using dialogName.dismiss();

Also do a null check. That is:

if (dialogName != null) {
    dialogName.dismiss();
}
Jacob Sánchez
  • 390
  • 3
  • 15
Reejesh PK
  • 658
  • 1
  • 11
  • 27
5

Another scenario where this error occurs is when the app crushes while a dialog is shown. Look above this error for additional errors.

Sir Codesalot
  • 7,045
  • 2
  • 50
  • 56
2

The problem is AsyncTask.

new AsyncTask<Void, Void, String>() {

    CustomDialog mProgressBar = new CustomDialog(MobileConnectActivity.this, R.style.dialog);
    .... 
}

You create a progress bar inside async task which makes a problem when you move to another activity.

For this before dismiss or show()

if(!isFinishing() && dialog != null) { dialog.dismiss();}

Usage of isFinishing()

 * Check to see whether this activity is in the process of finishing,
 * either because you called {@link #finish} on it or someone else
 * has requested that it finished.  This is often used in
 * {@link #onPause} to determine whether the activity is simply pausing or
 * completely finishing.
 *
 * @return If the activity is finishing, returns true; else returns false.
Arul
  • 1,031
  • 13
  • 23
0

Simply dismissing the dialog was not enough to get rid of the error for me. It turns out my code was holding onto a reference to the dialog even after it was dismissed. The key for me was to set that reference to null after dismissing the dialog.

Matt Mendrala
  • 211
  • 2
  • 3
0

The problem for me was happening because I used the android:noHistory="true" for the activity that was causing the problem, and it only happened on Android 6.0 API 23.

hiddeneyes02
  • 2,562
  • 1
  • 31
  • 58