AlertDialog.Builder无法解析为某种类型

时间:2012-10-26 02:02:38

标签: java android android-alertdialog

所以我正在尝试构建一个显示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();

    }
}

2 个答案:

答案 0 :(得分:3)

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

您的问题的简单解决方案是将您的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();

    }
}

答案 1 :(得分:3)

因为您的班级名称是AlertDialog。在你的onCreateDialog()函数中,

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

在这一行中,“AlterDialog”实际上是对你自定义的AlterDialog类的引用。如果你改为这个,它应该是有用的。

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