【问题标题】:AlerDialog is not created - java.lang.IllegalArgumentException: Activity#onCreateDialog did not create a dialog for id X未创建 AlerDialog - java.lang.IllegalArgumentException: Activity#onCreateDialog 没有为 id X 创建对话框
【发布时间】:2017-09-24 05:39:18
【问题描述】:

我想创建一个普通的 AlertDialog。我使用了 android dev 文档提供的示例。我刚刚将 DIALOG_PAUSED_ID 更改为 DIALOG_DELETEDB。如果我执行我的代码并按下应该创建对话框的按钮,我会收到以下错误日志:

04-29 01:01:20.973: WARN/dalvikvm(1168): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-29 01:01:20.973: ERROR/AndroidRuntime(1168): Uncaught handler: thread main exiting due to uncaught exception
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): java.lang.IllegalArgumentException: Activity#onCreateDialog did not create a dialog for id 4
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at android.app.Activity.createDialog(Activity.java:871)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at android.app.Activity.showDialog(Activity.java:2483)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at mjb.project.AVV.Favs.onMenuItemSelected(Favs.java:111)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:525)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at android.view.View.onTouchEvent(View.java:4179)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at android.widget.TextView.onTouchEvent(TextView.java:6540)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at android.view.View.dispatchTouchEvent(View.java:3709)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at android.os.Looper.loop(Looper.java:123)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at android.app.ActivityThread.main(ActivityThread.java:4363)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at java.lang.reflect.Method.invokeNative(Native Method)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at java.lang.reflect.Method.invoke(Method.java:521)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168):     at dalvik.system.NativeStart.main(Native Method)

所以这里是“相关”代码部分:

定义 ID:

private static final int DELETE_DB_ID   = 3;
private Dialog dialog;
static final int DIALOG_DELETEDB = 4;

onCreateDialog(...):

protected Dialog onCreateDialog(int id) {
        switch(id) {
        case DIALOG_DELETEDB:
            // do the work to define the pause Dialog
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Are you sure you want to exit?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                     Favs.this.finish();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                     dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
            break;
        default:
            dialog = null;
        }


return dialog;
    }

在这里我尝试“创建”对话框:

@Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
        case ADD_ID:
            createNote();
            return true;
        case DELETE_DB_ID:
            showDialog(DIALOG_DELETEDB);
            return true;
        }      
        return super.onMenuItemSelected(featureId, item);
    }

正如我已经说过的,我只是复制了代码并更改了名称。不幸的是,我不明白错误日志消息..:/不知何故我认为我没有返回创建的对话框,但我看不到我的参考“在哪里”或我必须返回的位置/内容......

提前感谢您的帮助。

【问题讨论】:

    标签: java android android-alertdialog


    【解决方案1】:
    1. 不应在 onCreateDialog 中调用 alert.show()。
    2. 您的 onCreateDIalog 返回的对话框变量实际上为空,您无需对其进行初始化。

    请看一下 samples\ApiDemos\src\com\example\android\apis\app\AlertDialogSamples.java,它在那里正确完成。 您也可以阅读此http://developer.android.com/guide/topics/ui/dialogs.html 以了解正确的 onCreateDialog 用法。 这是您的 onCreateDialog 的固定版本:

    protected Dialog onCreateDialog(int id) {
            switch(id) {
            case DIALOG_DELETEDB:
                // do the work to define the pause Dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Are you sure you want to exit?")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                         Favs.this.finish();
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                         dialog.cancel();
                    }
                });
                AlertDialog alert = builder.create();
                return alert;
            }
    
    return null;
        }
    

    另一种方法是直接显示对话框:

    public boolean onMenuItemSelected(int featureId, MenuItem item) {
            switch(item.getItemId()) {
            case DELETE_DB_ID:
              AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Are you sure you want to exit?")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                         Favs.this.finish();
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                         dialog.cancel();
                    }
                });
                AlertDialog alert = builder.create();
                alert.show();
                return true;
            }      
            return super.onMenuItemSelected(featureId, item);
        }
    

    【讨论】:

    • 奇怪..我尝试了“返回警报”,但也许我也更改了其他内容,所以我遇到了不同的问题。尽管如此,非常感谢你,它的工作原理! :-) 顺便提一句。我多次阅读了您提供的链接..,这就是我从中获取代码的地方..也许我只是累了..修复了它->床:-)
    【解决方案2】:

    别忘了先导入android.app.AlertDialog。

     AlertDialog alert = new AlertDialog.Builder(this).create();
                alert.setTitle("Error");
                alert.setMessage("Sorry, your device doesn't support flash light!");
                alert.setButton(Dialog.BUTTON_POSITIVE,"OK",new DialogInterface.OnClickListener(){
    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
    
    alert.show();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多