AlertDialog.builder不能解决为类型



因此,我正在尝试构建一个显示3行的弹出窗口:-时间 - 事件类型 - 位置

我有两个按钮,好的(这关闭了弹出窗口)并发送到映射(这向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。在您的otCreatedialog()函数中,

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

在这一行中,thie" AlterDialog"实际上是指您的自定义AlterDialog类。如果您对此进行更改,那应该是工作的。

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

最新更新