突然(这个项目代码没有任何变化)我开始得到一个错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.IllegalArgumentException: Button does not exist
该错误指向尚未调用的方法.
private void dialog(String title, String content){ AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); alertDialog.setTitle(title); alertDialog.setMessage(content); alertDialog.setCancelable(true); alertDialog.setButton(1, "OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alertDialog.show(); }
我试图在其他项目中复制并使用该代码 - 结果相同,并且它不久前工作(相同的目标API等).知道我在忽视什么吗?
不要硬编码1
在setButton(...)
.使用类中的常量DialogInterface
来指定哪个按钮:
DialogInterface.BUTTON_NEGATIVE
DialogInterface.BUTTON_POSITIVE
DialogInterface.BUTTON_NEUTRAL