0

我用这种方法加载对话框showDatePickerDialog(this.getCurrentFocus());。以前这样就好了。但是当我setOnClickListener在按钮单击时显示对话框时,出现编译器错误。错误是The method getCurrentFocus() is undefined for the type new View.OnClickListener(){}。我的代码如下

    frombutton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            showDatePickerDialog(this.getCurrentFocus());

        }
    });
4

1 回答 1

2

问题是,它new OnClickListener()创建了一个内部类,它不是 type Activity,就像外部类一样。

因此,this指的是OnClickListener,而不是您的活动。-getCurrentFocus()方法在侦听器类中不存在,因此无法找到。

解决这个问题的方法是明确地说你想要this来自周围(外部)类。您可以通过在之前添加外部类名称来做到这一点this

showDatePickerDialog(YOURACTIVITYCLASSNAMEHERE.this.getCurrentFocus());
于 2013-04-13T10:31:05.763 回答