ユーザーが新しい項目を入力してリストに追加できるように、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)
...