【发布时间】:2016-04-29 06:58:55
【问题描述】:
当我创建 AlertDialog 类对象,然后通过 new AlertDialog.builder(this) 比它给我下面的屏幕截图中显示的以下错误,如果我使用 builder 对象而不是 cancel() 函数不起作用并给我强制关闭错误在模拟器中
【问题讨论】:
当我创建 AlertDialog 类对象,然后通过 new AlertDialog.builder(this) 比它给我下面的屏幕截图中显示的以下错误,如果我使用 builder 对象而不是 cancel() 函数不起作用并给我强制关闭错误在模拟器中
【问题讨论】:
请更换,
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
因为AlertDialog 不是 AlertDialog.Builder()。您可以在下面找到创建AlertDialog 的完整示例:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Alert 1");
alertDialog.setMessage("This is an alert");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
AlertDialog alert = alertDialog.create();
alert.show();
【讨论】:
使用
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
改为。
【讨论】:
试试这个改变:
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
并参考此链接 :http://developer.android.com/guide/topics/ui/dialogs.html
【讨论】:
AlertDialog 存在于两个类中:android.app.AlertDialog, android.support.v7.app.AlertDialog。请确保您在课堂上使用相同的 AlertDialog。
【讨论】:
下面应该可以工作
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
【讨论】:
您正在使用 AlertDialog 而不是 AlertDialog.Builder。该错误(即 mainContentScreen:Cannot convert from AlertDialog.Builder to AlertDialog)清楚地表明您在哪里犯了错误。请仔细阅读错误。
【讨论】: