Java AlertDialog.Builder无法解析为类型

Java AlertDialog.Builder无法解析为类型,java,android,android-alertdialog,Java,Android,Android Alertdialog,因此,我尝试构建一个弹出窗口,其中显示3行: -时间 -事件类型 -地点 然后我有两个按钮,OK(这关闭了弹出窗口)和Send to Map(这向Google Maps提交了一个明确的意图并将位置发送给它,我还没有编写此代码) 出于某种奇怪的原因,我在Eclipse中遇到一个错误,它说“AlertDialog.Builder无法解析为一个类型”。我假设我已经正确导入了它,并多次清理了它。我不知道如何继续。谢谢你的帮助 import android.R; import android.app.Di

因此,我尝试构建一个弹出窗口,其中显示3行: -时间 -事件类型 -地点

然后我有两个按钮,OK(这关闭了弹出窗口)和Send to Map(这向Google Maps提交了一个明确的意图并将位置发送给它,我还没有编写此代码)

出于某种奇怪的原因,我在Eclipse中遇到一个错误,它说“AlertDialog.Builder无法解析为一个类型”。我假设我已经正确导入了它,并多次清理了它。我不知道如何继续。谢谢你的帮助

import android.R;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class AlertDialog 
{
public Dialog onCreateDialog(Bundle savedInstanceState) 
{
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Time: " + SMSReceiver.getTime() + "\nIncident: " + 
    SMSReceiver.getCallType() + "\nLocation: " + SMSReceiver.getAddress())
    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {


        }
    })
    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {


        }
    });

    return builder.create();

    }
}

这实际上不是一个错误,您错误地使用AlertDialog创建了一个类名,它实际上已经驻留在android包中。现在,当您使用AlertDialog创建类并尝试访问其生成器方法时,它会给您一个错误,因为您的自定义类没有该方法

解决问题的简单方法是将AlertDialog类重命名为其他类名,这样问题就会得到解决

注意:代码中没有其他错误

我建议您将类名更改为任何其他名称,例如说MyAlertDialog,那么您的类代码如下所示(您还需要按照Java文件命名约定的规则,根据公共类更改文件名

public class MyAlertDialog // See change is here
{
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Time: " + SMSReceiver.getTime() + "\nIncident: " + 
                SMSReceiver.getCallType() + "\nLocation: " + SMSReceiver.getAddress())
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {


                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {


                    }
                });

        return builder.create();

    }
}

这实际上不是一个错误,您错误地使用AlertDialog创建了一个类名,该类名实际上已经存在于android软件包中。现在,当您使用AlertDialog创建该类并尝试访问其生成器方法时,它会给您一个错误,因为您的自定义类没有该方法

解决问题的简单方法是将AlertDialog类重命名为其他类名,这样问题就会得到解决

注意:代码中没有其他错误

我建议您将类名更改为任何其他名称,例如说MyAlertDialog,那么您的类代码如下所示(您还需要按照Java文件命名约定的规则,根据公共类更改文件名

public class MyAlertDialog // See change is here
{
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Time: " + SMSReceiver.getTime() + "\nIncident: " + 
                SMSReceiver.getCallType() + "\nLocation: " + SMSReceiver.getAddress())
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {


                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {


                    }
                });

        return builder.create();

    }
}

因为类名是AlertDialog。在onCreateDialog()函数中

AlertDialog.Builder builder = new AlertDialog.Builder(this);
在这一行中,“AlterDialog”实际上是对您自定义的AlterDialog类的引用。如果您更改为该类,它应该是工作的

android.app.AlertDialog.Builder builter = new android.app.AlertDialog.Builder(this);

因为类名是AlertDialog。在onCreateDialog()函数中

AlertDialog.Builder builder = new AlertDialog.Builder(this);
在这一行中,“AlterDialog”实际上是对您自定义的AlterDialog类的引用。如果您更改为该类,它应该是工作的

android.app.AlertDialog.Builder builter = new android.app.AlertDialog.Builder(this);

AlertDialog
是Android软件包的内置类,您不能使用该名称,请尝试使用其他名称AlertDialog1,如Forgive me,以免混淆,但您建议将其更改为:AlertDialog1.Builder=new AlertDialog1.Builder(此);这是正确的吗?不,您的类名应该更改为AlertDialog1,您的类文件名也应该更改
AlertDialog
是Android软件包的内置类,您不能使用该名称,请尝试其他名称AlertDialog1,如图所示,以防混淆,但您建议将其更改为:AlertDialog1.Builder builder=new AlertDialog1.Builder(此);是否正确?不,您的类名应更改为AlertDialog1,并且您的类文件名应更改为ToAnk you!这就解决了所有问题。我完全忘记了我的类名与此共享。AlertDialog.Builder=new AlertDialog.Builder(某个);谢谢!这就解决了所有问题。我完全忘记了我的类名与此共享。AlertDialog.Builder=new AlertDialog.Builder(某物);