0

I'm popping up an AlertDialog so users can type in a new item to add to a list. However, whenever the screen orientation changes, the LogCat spits out a bunch of errors regarding a window leak. I can't quite figure out what I'm doing wrong here.

private void launchPopup() {
    if (mTypedText == null) {
        mTypedText = "";
    }

    LayoutInflater inflater = (LayoutInflater) ActivityTags.this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.layout_additem, (ViewGroup) findViewById(R.id.additem_root));

    final TextView text = (TextView)layout.findViewById(R.id.additem_text);
    text.setText("Type in the name of the new item.");
    text.setLines(1);

    final EditText name = (EditText)layout.findViewById(R.id.additem_name);
    name.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {}

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        public void afterTextChanged(Editable s) {
            if (s != null) {
                mTypedText = s.toString();
            }
            else {
                mTypedText = null;
            }
        }
    });
    if (mTypedText != null) {
        name.setText(mTypedText);
    }

    mAddDialog = new AlertDialog.Builder(ActivityItems.this);
    mAddDialog.setTitle("Add Item");
    mAddDialog.setView(layout);
    mAddDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialogInterface, int i) {
        // TODO
    }
});
    mAddDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialogInterface, int i) {
        mTypedText = null;
    }
});
    mAddDialog.show();
}

In onStop(), mAddDialog is set to null if it is not already null.

Here is the error:

Activity com.myapp.app.activity.ActivityItems has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@45164f30 that was originally added here
android.view.WindowLeaked: Activity com.myapp.app.activity.ActivityItems has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@45164f30 that was originally added here
at android.view.ViewRoot.<init>(ViewRoot.java:247)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.app.Dialog.show(Dialog.java:241)
at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
...
Andrew
  • 20,756
  • 32
  • 99
  • 177
  • http://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added – Samir Mangroliya Jun 05 '12 at 15:28
  • AlertDialog.Builder does not provide me with a dismiss() method, so how am I supposed to get rid of it before leaving the Activity? – Andrew Jun 05 '12 at 15:32
  • hmm create alertdialog Object you just create Builder Object so ...`AlertDialog alert =mAddDialog.create()` – Samir Mangroliya Jun 05 '12 at 15:35
  • I have done this in onStop(): `AlertDialog alert = mAddDialog.create(); alert.dismiss(); mAddDialog = null`. However, I am still receiving the same error. – Andrew Jun 05 '12 at 15:41
  • you must consider thsi comment :: Also- this error can be a little misleading in some circumstances - i.e. in my case an unhandled Exception was thrown in an AsyncTask, which caused the Activity to shutdown, then an open progress dialog caused this Exception.. so the 'real' exception was a little earlier in the log – Bobby Jan 28 '11 at 2:00 – Umar Qureshi Jun 05 '12 at 15:44
  • There are no other exceptions shown in the log and there are no AsyncTasks running – Andrew Jun 05 '12 at 15:52
  • You can use the create() method to save the dialog and than show() on the dialog – idog Jan 08 '14 at 16:57

0 Answers0