【问题标题】:AlertDialog = new AlertDialog.builder(this) gets typecast errorAlertDialog = new AlertDialog.builder(this) 获取类型转换错误
【发布时间】:2016-04-29 06:58:55
【问题描述】:

当我创建 AlertDialog 类对象,然后通过 new AlertDialog.builder(this) 比它给我下面的屏幕截图中显示的以下错误,如果我使用 builder 对象而不是 cancel() 函数不起作用并给我强制关闭错误在模拟器中

【问题讨论】:

    标签: android dialog


    【解决方案1】:

    请更换,

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

    【讨论】:

      【解决方案2】:

      使用

      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 
      

      改为。

      【讨论】:

        【解决方案3】:

        试试这个改变:

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

        并参考此链接 :http://developer.android.com/guide/topics/ui/dialogs.html

        【讨论】:

          【解决方案4】:

          AlertDialog 存在于两个类中:android.app.AlertDialog, android.support.v7.app.AlertDialog。请确保您在课堂上使用相同的 AlertDialog

          【讨论】:

            【解决方案5】:

            下面应该可以工作

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

            【讨论】:

              【解决方案6】:

              您正在使用 AlertDialog 而不是 AlertDialog.Builder。该错误(即 mainContentScreen:Cannot convert from AlertDialog.Builder to AlertDialog)清楚地表明您在哪里犯了错误。请仔细阅读错误。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2022-07-13
                • 1970-01-01
                • 2014-03-03
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多