开发者

AlertDialog fails ... sometimes

I have a ListActivity containing items that contain some text and a button. When the button is pressed I may need to display an AlertDialog to ask the user if he's sure he wants to perform the action. S开发者_如何学JAVAo here's the activity's onCreateDialog:

@Override
protected Dialog onCreateDialog(int id)
    {
    switch (id)
        {
        case CONFIRM_DELETE_DLG:
            {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(getString(R.string.remove_confirmation))
                   .setCancelable(false)
                   .setPositiveButton(R.string.caption_yes, this)
                   .setNegativeButton(R.string.caption_no, this);
            return builder.create();
            }
        default:
            break;
        }
    return null;
    }

(There's only one type of dialog, so this function is overkill, but it works.)

When a button in the dialog is pressed, my onClick(DialogInterface dialog, int which) function is called. It does what it needs to do to process the delete operation, then calls dismissDialog(CONFIRM_DELETE_DLG).

Finally, in my Activity's onDestroy() I call removeDialog(CONFIRM_DELETE_DLG) in a try/catch block that allows me to ignore the situation where the dialog has never been created.

PROBLEM:

Usually the first time I invoke the dialog, it works. About every other time I enter this ListActivity and invoke this dialog, I get the following on the builder.create() call:

Attempted to add application window with unknown token

The only discussion I've seen online is related to the context passed to AlertDialog.Builder(). Every supposedly working example I've seen uses the Activity's "this" member as the parameter, and that's what I'm using.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜