2011-10-01 2 views
0

J'ai le code suivant appelé quand un bouton est enfoncé dans mon activité.Vous avez besoin de suggestion pour créer un AlertDialog dans Android

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

J'ai ajouté des importations, ces deux:

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

Mais obtenez toujours des erreurs suivantes:

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 

Quelqu'un peut-il donner une solution à me s'il vous plaît? Je suis très nouveau à Java.

Répondre

3

Créer la AlertDilog façon suivante:

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