Android AlertDialog=new AlertDialog.builder(此)获取类型转换错误

Android AlertDialog=new AlertDialog.builder(此)获取类型转换错误,android,dialog,Android,Dialog,当我创建AlertDialog类对象,然后使用new AlertDialog.builder(此)时,它会给我以下错误,如下面的屏幕截图所示,如果我使用builder对象,则cancel()函数不起作用,并在emulator中给我强制关闭错误 请更改 AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 因为AlertDialogist不是AlertDialog.Builder()。您可以在下面找到创建警报对话框的完

当我创建AlertDialog类对象,然后使用new AlertDialog.builder(此)时,它会给我以下错误,如下面的屏幕截图所示,如果我使用builder对象,则cancel()函数不起作用,并在emulator中给我强制关闭错误

请更改

AlertDialog alertDialog = new AlertDialog.Builder(this).create();
因为
AlertDialog
ist不是
AlertDialog.Builder()
。您可以在下面找到创建
警报对话框的完整示例:

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();
使用


相反。

警报对话框有两类:
android.app.AlertDialog,android.support.v7.app.AlertDialog
。请确保您在课堂上使用相同的AlertDialog

您使用的是AlertDialog,而不是AlertDialog.Builder。该错误(即mainContentScreen:无法从AlertDialog.Builder转换为AlertDialog)清楚地显示了错误的位置。请仔细阅读错误。

尝试以下更改:

AlertDialog alertDialog = new AlertDialog.Builder(this).create();
并参考此链接 :

下面应该可以

AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this); 
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);