Android 带有按钮的警报对话框引发异常

Android 带有按钮的警报对话框引发异常,android,android-dialog,Android,Android Dialog,我正在通过执行以下操作显示警报对话框: new AlertDialog.Builder(this) .setTitle(R.string.label_searching) .setMessage(R.string.label_search_noresults) .setCancelable(false) .setPositiveButton(DialogInterface.BUTTON_POS

我正在通过执行以下操作显示警报对话框:

    new AlertDialog.Builder(this)
            .setTitle(R.string.label_searching)
            .setMessage(R.string.label_search_noresults)
            .setCancelable(false)
            .setPositiveButton(DialogInterface.BUTTON_POSITIVE, null)
            .create().show();
但是,会引发此异常:

致命异常:主 android.content.res.Resources$NotFoundException:字符串资源ID 0xffffffff 位于android.content.res.Resources.getExtresources.java:242 位于android.content.Context.getTextContext.java:282 在android.app.AlertDialog$Builder.setPositiveButtonAlertDialog.java:487

当我注释掉以下行时:

.setPositiveButton(DialogInterface.BUTTON_POSITIVE, null)
对话框显示,但显然没有显示按钮。我需要在对话框中显示一个按钮


我做错了什么?

我认为DialogInterface.BUTTON\u有问题
您可以在DialogInterface中找到解决方案。如果您选中DialogInterface类,则BUTTON_正数为常量:

int BUTTON_POSITIVE = -1;
setPositiveButton方法为其参数接收到一个textId或一个charsequence,该参数是将显示在PositiveButton上的文本。Android找不到DialogInterface定义的-1 id的关联字符串

我建议您在xml文件中定义积极按钮文本,就像您在标题和消息标签中所做的那样,并在第一个参数中使用它。

相应地使用它


你需要像这样获取字符串id

new AlertDialog.Builder(this)
    .setTitle(getResources().getString(R.string.label_searching))
    .setMessage(getResources().getString(R.string.label_search_noresults))
    .setCancelable(false)
    .setPositiveButton(DialogInterface.BUTTON_POSITIVE, null)
    .create().show();

setPositiveButtonOk,null是否有效?请给出一个字符串并重试。为它添加一个侦听器
private void createAlertDialog() {

     AlertDialog.Builder alrtDialog = new AlertDialog.Builder(
                this);
        alrtDialog.setMessage("your message").setCancelable(false);
        alrtDialog.setPositiveButton("ButtonName",
                new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Do somthing
            }
        });

        alrtDialog.setNeutralButton(R.string.cancel,
                new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                //Do somthing
            }
        });
        alrtDialog.create();
        alrtDialog.show();
    }
new AlertDialog.Builder(this)
    .setTitle(getResources().getString(R.string.label_searching))
    .setMessage(getResources().getString(R.string.label_search_noresults))
    .setCancelable(false)
    .setPositiveButton(DialogInterface.BUTTON_POSITIVE, null)
    .create().show();