2012-01-11 12 views
13

Próbuję uruchomić AlertDialog z onClickListener, ale pojawia się następujący błąd.AlertDialog wewnątrz onClickListener

The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined 

Czy ktoś wie, jak to naprawić?

 mRecordButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      new AlertDialog.Builder(this) 
      .setTitle("Cast Recording") 
      .setMessage("Now recording your message") 
      .setPositiveButton("Save", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        Log.d("AlertDialog", "Positive"); 
       } 
      }) 
      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        Log.d("AlertDialog", "Negative"); 
       } 
      }) 
      .show(); 
     } 
    }); 

Odpowiedz

29

Zmiana ta linia

new AlertDialog.Builder(this); 

do

new AlertDialog.Builder(YourActivity.this); 

To dlatego, że konstruktor potrzebuje typu Context & OnclickListner is not a Context type więc korzystania z przedmiotu swojej działalności.

Mam nadzieję, że to pomaga ..

0

W new AlertDialog.Builder(this), this odnosi się do słuchacza, a nie zewnętrznej instancji klasy.

Powiązane problemy