-1

im having a small issue, i am trying to implement a Date picker using a fragment into another fragment but i get this error message when i click the button to load the Date picker i get that error in the logs

java.lang.ClassCastException: com.example.buzzamaid.HomeActivity cannot be cast to android.app.DatePickerDialog$OnDateSetListener
        at com.example.buzzamaid.Fragments.DatePickerFragment.onCreateDialog(DatePickerFragment.java:23)

i am assuming its trying to pass it into the HomeActivity instead of the DateTimeFragment that i created. how can i fix it?

my datepickerfragment


    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

        Calendar c = Calendar.getInstance();
        int year =  c.get(Calendar.YEAR);
        int month =  c.get(Calendar.MONTH);
        int day =  c.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getContext(),(DatePickerDialog.OnDateSetListener) getContext(), year, month, day);

    }
}

MyDateTimeFragment



    Unbinder unbinder;

    @BindView(R.id.bt_select_date)
    Button bt_select_date;

    @BindView(R.id.bt_select_time)
    Button bt_select_time;

    @BindView(R.id.tv_selected_date)
    TextView tv_selected_date;

    @BindView(R.id.tv_selected_time)
    TextView tv_selected_time;



    static DateTimeFragement instance;

    public  static DateTimeFragement getInstance() {
        if (instance == null)
            instance = new DateTimeFragement();
        return instance;

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View itemView =  inflater.inflate(R.layout.fragment_date_time,container,false);
        unbinder = ButterKnife.bind(this,itemView);

        return itemView;



    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);




    }


    @OnClick(R.id.bt_select_date)
    public void onClick() {
        DialogFragment datePicker = new DatePickerFragment();
        datePicker.show(getFragmentManager(), "Date Picker");
    }






    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        String currentDateString = DateFormat.getDateInstance(DateFormat.FULL).format(c.getTime());
        tv_selected_date.setText(currentDateString);
    }
}

How can i make it so it show the calender instead of crashing when i click the button in the fragment?

Emil C.
  • 101
  • 1
  • 11
  • Check documentation for second parameter of DatePickerDialog's constructor type ... and pass there something that implements it – Selvin Jul 01 '19 at 13:02

2 Answers2

0

If you have implemented OnDateSetListener to DatePickerFragment, change the DatePickerDialog instance as:

return new DatePickerDialog(getContext(), DatePickerFragment.this , year, month, day);
Deˣ
  • 4,191
  • 15
  • 24
0

Is your HomeActivity implementing DatePickerDialog.OnDateSetListener interface? If not please make sure make your HomeActivity implements DatePickerDialog.OnDateSetListener. Implementing this method will give you override method

@Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

    }

You can override this method based on your requirement.

Apurv
  • 391
  • 2
  • 9