我正在构建一个带有选择列表的警告对话框。我不明白为什么我的 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();
}
它在另一个类中起作用的原因是 actionListener
引用是一个类成员变量,正如我所怀疑的那样。
请注意,声明不在任何方法内。你可以对你的新 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();
}
关于java - DialogInterface.OnClickListener 监听器变量无法解析 [java],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36297110/