Surjya Narayana Padhi
Surjya Narayana Padhi

Reputation: 7841

Need suggestion in creating an AlertDialog in Android

I have the following code called when a button is pressed in my activity.

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this).create();  
            alertDialog.setTitle("Alert 1");  
            alertDialog.setMessage("This is an alert");  
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {  
              public void onClick(DialogInterface dialog, int which) {  
                return;  
            } });  

I have added imports these two :

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;

But still get following errors :

Description Resource    Path    Location    Type
The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined  MobileTrackerActivity.java  /MobileTracker/src/com/example/mobiletracker    line 77 Java Problem
The method setButton(String, new DialogInterface.OnClickListener(){}) is undefined for the type AlertDialog.Builder MobileTrackerActivity.java  /MobileTracker/src/com/example/mobiletracker    line 80 Java Problem
Type mismatch: cannot convert from AlertDialog to AlertDialog.Builder   MobileTrackerActivity.java  /MobileTracker/src/com/example/mobiletracker    line 77 Java Problem

Can anybody give any solution to me please? I am very new to Java.

Upvotes: 0

Views: 3286

Answers (2)

Ron
Ron

Reputation: 24235

Create the AlertDilog this way :

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

Upvotes: 3

James Black
James Black

Reputation: 41838

If you look at this page:

http://developer.android.com/guide/topics/ui/dialogs.html

and the error message you get you just need to make this change:

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

Upvotes: 1

Related Questions