2016-03-29 11 views
1

Seçim listesiyle bir uyarı iletişim kutusu yapıyorum. OnClickListener değişkenimin neden çözümlenemediğini anlayamıyorum.DialogInterface.OnClickListener listener değişkeni çözümlenemiyor [java]

Kodu kendi başına ayrı bir etkinliğe koydum ve işe yarıyor ama ana etkinliğimin içinde değil.

public void categoryDialogShow(final Context context, String[] categoryOptions){ 
    final AlertDialog actions; 
    DialogInterface.OnClickListener actionListener = new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      //goto category list with which as the category 
     } 
     }; 

    AlertDialog.Builder categoryAlert = new AlertDialog.Builder(context); 
    categoryAlert.setTitle("Choose a Category"); 

    categoryAlert.setItems(categoryOptions, actionListener); 
    categoryAlert.setNegativeButton("Cancel", null); 
    actions = categoryAlert.create(); 


    actions.show(); 
} 

diğer çalışır nedeni:

public class MainActivity extends ActionBarActivity { 

    AlertDialog actions; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setTitle("Activity"); 
     Button button = new Button(this); 
     button.setText("Click for Options"); 
     button.setOnClickListener(buttonListener); 

     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Choose an Option"); 
     String[] options = { "A", "B", "C" }; 
     builder.setItems(options, actionListener); 
     builder.setNegativeButton("Cancel", null); 
     actions = builder.create(); 

     setContentView(button); 
     } 
     DialogInterface.OnClickListener actionListener = new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      switch (which) { 
      case 0: // Delete 
      break; 
      case 1: // Copy 
      break; 
      case 2: // Edit 
      break; 
      default: 
      break; 
      } 
     } 
     }; 
     View.OnClickListener buttonListener = new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      actions.show(); 
     } 
     }; 
} 

cevap

2

Sadece başvuru gerekiyor nerede yukarıdaki beyanı taşıyın:

public void categoryDialogShow(final Context context, String[] categoryOptions){ 
     final AlertDialog actions; 

     AlertDialog.Builder categoryAlert = new AlertDialog.Builder(context); 
     categoryAlert.setTitle("Choose a Category"); 

     categoryAlert.setItems(categoryOptions, actionListener); 
//============================================================= 
//==============actionListener cannot be resolved to a variable 
//============================================================= 
     categoryAlert.setNegativeButton("Cancel", null); 
     actions = categoryAlert.create(); 

     DialogInterface.OnClickListener actionListener = new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      //goto category list with which as the category 
     } 
     }; 
     actions.show(); 
} 

Bu

herhangi bir sorun ile çalışır aktivitedir sınıf, actionListener başvurusunun, şüphelendiğim gibi bir sınıf üyesi değişkeni olduğunu. Bildirimin herhangi bir yöntem içinde olmadığına dikkat edin. Eğer yeni Aktivite ile aynı yapabilirdi:

DialogInterface.OnClickListener actionListener = new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      //goto category list with which as the category 
     } 
     }; 

public void categoryDialogShow(final Context context, String[] categoryOptions){ 
    final AlertDialog actions; 

    AlertDialog.Builder categoryAlert = new AlertDialog.Builder(context); 
    categoryAlert.setTitle("Choose a Category"); 

    categoryAlert.setItems(categoryOptions, actionListener); 
    categoryAlert.setNegativeButton("Cancel", null); 
    actions = categoryAlert.create(); 


    actions.show(); 
} 
+0

Evet, bildirimin taşınması. Sorgumu, kullandığım öğretici komut dosyasıyla düzenledim. Orada neden işe yarayacağını görebiliyor musun? – silversunhunter

+0

@silversunhunter sadece cevabı güncelledi, asıl tahminim doğruydu. –

+0

Bu bir kapsam sorunu mu? Örnekte, dialogInterface oncreate() öğesinin dışındayken, komutumda dialogInterface yöntemde yer alır. – silversunhunter

1

Ya

DialogInterface.OnClickListener actionListener = new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      //goto category list with which as the category 
     } 
     }; 

önce

categoryAlert.setItems(categoryOptions, actionListener); 

beyan veya yöntemin dışında beyan.

+0

Yanıt için teşekkür ederiz. Haklısın, beyannameyi hareket ettir! – silversunhunter

İlgili konular