更改方向时泄漏窗口

时间:2012-06-05 15:27:42

标签: android alertdialog

我正在弹出一个AlertDialog,这样用户就可以输入一个新项目来添加到列表中。但是,每当屏幕方向发生变化时,LogCat会发出一系列有关窗口泄漏的错误。我无法弄清楚我在这里做错了什么。

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();
}

在onStop()中,如果mAddDialog不是null,则设置为null。

这是错误:

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)
...

0 个答案:

没有答案