DialogInterface.OnClickListener 无法解析侦听器变量 [java]

DialogInterface.OnClickListener listener variable cannot be resolved [java]

我正在构建一个带有选择列表的警告对话框。我不明白为什么我的 OnClickListener 变量无法解析。

我已将代码单独放在一个单独的 activity 中,它可以工作,但在我的主 activity 中却没有。

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

这是可以正常工作的 activity:

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

只需将声明移到需要引用它的上方即可:

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

它在另一个 class 中起作用的原因是 actionListener 引用是一个 class 成员变量,正如我所怀疑的那样。 请注意,声明不在任何方法内。您可以对新的 Activity:

做同样的事情
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();
}

要么声明

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

之前

 categoryAlert.setItems(categoryOptions, actionListener);

或者在你的方法之外声明它。