简体   繁体   English

Android:对话强制在创建时关闭

[英]Android: Dialog force closes on create

Goal is to make a dialog that appears on menu_key pressed, but it keeps force closing. 目标是使出现在menu_key上的对话框被按下,但保持强制关闭。 I consulted the official FAQ on Dialogs , but no luck. 我查阅了有关Dialogs的官方常见问题解答 ,但没有运气。 Everything was done as explained there, but it still fails as soon as the button is pressed. 一切都按照此处的说明进行,但是一旦按下按钮仍然失败。 Here is the creation of the dialog: 这是对话框的创建:

static final int Choice_ID = 0;

    protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;
        switch(id) {
        case Choice_ID:
            // do the work to define the pause Dialog
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("TEST DIALOG")
                   .setCancelable(true);

            AlertDialog alert = builder.create();
            break;

        //default:
            //dialog = null;
        }
        return dialog;
    }

And the part about showing looks like this: 关于显示的部分如下所示:

public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_MENU) {
            showDialog(Choice_ID);

        };
};

This should not cause a force close, but you can see the menu if you try setting the value of dialog : 这不会导致强制关闭,但是如果您尝试设置dialog的值,则可以看到菜单:

dialog = builder.create();

As in: 如:

protected Dialog onCreateDialog(int id) {
    Dialog dialog = null;
    switch(id) {
    case Choice_ID:
        // do the work to define the pause Dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("TEST DIALOG")
               .setCancelable(true);

        dialog = builder.create();
        break;

    //default:
        //dialog = null;
    }
    return dialog;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM