gpt4 book ai didi

android - AlertDialog 按钮不存在

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:33:26 26 4
gpt4 key购买 nike

突然(本项目代码没有任何改动)开始报错:

java.lang.RuntimeException: Unable to start activity ComponentInfo{<package>}: 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 等)。知道我忽略了什么吗?

最佳答案

不要在 setButton(...) 中硬编码 1 .使用在 DialogInterface 中找到的常量指定哪个按钮的类:

DialogInterface.BUTTON_NEGATIVE

DialogInterface.BUTTON_POSITIVE

DialogInterface.BUTTON_NEUTRAL

关于android - AlertDialog 按钮不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20725199/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com