Android AlertDialog按钮不存在

Android AlertDialog按钮不存在,android,android-dialog,Android,Android Dialog,突然(此项目代码没有任何更改)我开始获得一个错误: java.lang.RuntimeException: Unable to start activity ComponentInfo{<package>}: java.lang.IllegalArgumentException: Button does not exist 我尝试在其他项目中复制并使用该代码-结果相同,不久前它还在工作(相同的目标API等)。知道我忽略了什么吗?不要硬编码1。使用类中的常量指定哪个按钮: 更改此

突然(此项目代码没有任何更改)我开始获得一个错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{<package>}: java.lang.IllegalArgumentException: Button does not exist

我尝试在其他项目中复制并使用该代码-结果相同,不久前它还在工作(相同的目标API等)。知道我忽略了什么吗?

不要硬编码
1
。使用类中的常量指定哪个按钮:

更改此行:

alertDialog.setButton(1, "OK", new DialogInterface.OnClickListener() {
其中一项:

alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "OK", new DialogInterface.OnClickListener() {
alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
您可以查看和上的Android文档以查看
setButton
方法

您还可以用它们的常量值分别替换
按钮正
按钮负
按钮中性
-1
-2
-3

例如:

// positive button
alertDialog.setButton(-1, "OK", new DialogInterface.OnClickListener() {
// positive button
alertDialog.setButton(-1, "OK", new DialogInterface.OnClickListener() {