1

I think I is just a small mistake but I cant find it. In the following code my whole @Override method is marked as "false".

        inputBill.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                final Calendar calendar = Calendar.getInstance();
                mDay = calendar.get(Calendar.DAY_OF_MONTH);
                mMonth = calendar.get(Calendar.MONTH);
                mYear = calendar.get(Calendar.YEAR);

                DatePickerDialog dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        inputBill.setText(dayOfMonth + "." + monthOfYear + "." + year);
                    }
                }, mYear, mMonth, mDay);
            dpd.show();
            }
        });
Rod Kimble
  • 1,302
  • 3
  • 18
  • 44

3 Answers3

2

You can not use instance of View.OnFocusChangeListener to show dialog. For Alerts you always need Activity context, So you need to use current activity instance there.


Problem is

DatePickerDialog dpd = new DatePickerDialog(**this**, new DatePickerDialog.OnDateSetListener() {

use

DatePickerDialog dpd = new DatePickerDialog(ACTIVITY_NAME.this, new DatePickerDialog.OnDateSetListener() {
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
1

Edited

You can use getContext()

 DatePickerDialog dpd = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() 
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

As a side note to @Pankaj Kumar's answer:

Whenever you instantiate an anonymous class, inside that class this refers to the instantiating class not your main class.

public class MainClass{
    public method(){
        new AnotherClass(){
            public someMethod(){
                Object o1 = this;
                Object o2 = MainClass.this;
            }
        }
    }
}

In this example:

  • o1: Points to current instance of AnotherClass.

  • o2: Points to current instance of MainClass.

frogatto
  • 28,539
  • 11
  • 83
  • 129