AndresL
AndresL

Reputation: 80

AlertDialog.Builder cannot be resolved to a type

So I'm trying to build a popup window that displays 3 lines: -The time -The incident type -The location

I then have two buttons, OK (This closes the popup) and Send to Map (This submits an explicit intent to Google Maps and sends the location to it, I have yet to write this code)

For some strange reason, I get an error in Eclipse where it says "AlertDialog.Builder cannot be resolved to a type." I assume I've imported it correctly, as well as cleaned it multiple times. I'm unsure how to proceed. Thank you for your assistance.

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

    }
}

Upvotes: 0

Views: 9359

Answers (2)

Race
Race

Reputation: 414

Because your class name is AlertDialog. In your onCreateDialog() function,

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

In this line, thie "AlterDialog" is actually reference to your self defined AlterDialog class. If you change to this, it should be work.

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

Upvotes: 3

Lucifer
Lucifer

Reputation: 29632

Its actually not an error, you have by mistake created a class name with AlertDialog which is actually already resides in the android Package. Now when you have created the class with AlertDialog and you are trying to access its Builder method it gives you an error, because your custom class doesnt have that method.

Simple solution for your question is just rename your AlertDialog class to some other class name and your problem will get solve.

Note : there is no other error in your code.

I suggest you to change your class name to any other name for example say MyAlertDialog, then your class code will be like below, ( also you need to change your file name according your public class as rule of Java File naming convention,

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

    }
}

Upvotes: 3

Related Questions