Нужно предложение при создании AlertDialog в Android - PullRequest
0 голосов
/ 01 октября 2011

У меня следующий код вызывается при нажатии кнопки в моей деятельности.

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

Я добавил импорт этих двух:

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

Но все равно получаю следующие ошибки:

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

Кто-нибудь может дать мне какое-нибудь решение, пожалуйста?Я очень новичок в Java.

Ответы [ 2 ]

3 голосов
/ 01 октября 2011

Создайте AlertDilog следующим образом:

   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();
1 голос
/ 01 октября 2011

Если вы посмотрите на эту страницу:

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

и полученное сообщение об ошибке, вам просто нужно внести это изменение:

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