簡體   English   中英

Android:對話強制在創建時關閉

[英]Android: Dialog force closes on create

目標是使出現在menu_key上的對話框被按下,但保持強制關閉。 我查閱了有關Dialogs的官方常見問題解答 ,但沒有運氣。 一切都按照此處的說明進行,但是一旦按下按鈕仍然失敗。 這是對話框的創建:

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

關於顯示的部分如下所示:

public boolean onKeyDown(int keyCode, KeyEvent event) {

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

        };
};

這不會導致強制關閉,但是如果您嘗試設置dialog的值,則可以看到菜單:

dialog = builder.create();

如:

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