0

I am using DatePicker from one of the fragments (fragment corresponding to Tab 2) of the TabSwipeActivity that has 3 tabs. I have a button in the 2nd fragment which "onclick"ed, calls the showDatePickerDialog method as shown in the Android Developer site.

Referred to this link ClassCastException fragment android using ActionBar posted by Pablo as he also faced similar difficulties.

From Pablo's updated comment on Jun 9, he was suggesting that he did not implement Listener in DatePickerFragment. I followed his way and my DatePickerFragment, when removed of the listener portions, looks as below

public class DatePickerFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener)getActivity(), year, month, day);
    }

}

And Pablo says he had the fragment that calls the DatePicker dialog (in my case 2nd fragment) implemnent the listener interface and the onDateset method of the listener interface. So the 2nd fragment class code pertaining to the date picker is as below:-


public class CaptureFrag2 extends Fragment implements DatePickerDialog.OnDateSetListener {

//showDatePickerDialog is defined in the onClick attribute corresponding to btn_dob of Fragment 2

public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
}

@Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
        String dob;

        dob = String.valueOf(monthOfYear)+ "-" + String.valueOf(year)
                        + "-" + String.valueOf(dayOfMonth);

        btn_dob.setText(dob);

  }

I am getting this error.

java.lang.IllegalStateException: Could not find a method showDatePickerDialog(View) in the activity class com.androidexample.seetha.myapplication.Tabs for onClick handler on view class android.widget.Button with id 'capture_frag2_btn_dob'
                at android.view.View$1.onClick(View.java:3994)
                at android.view.View.performClick(View.java:4756)

Possible Area where the error is

It looks for showDatePickerDialog(View) in Tabs which is the main activity that implements TabListener whereas in my case I have this method in CaptureFrag2 (i.e. the second fragment of the Tab activity).

Should I implement OnDateSetListener in Tabs itself. It will help if some one who has implemented DatePicker Dialog from one of the fragments of Tab Swipe share the relevant code in all the 3 classes

Tabs (the one that implements TabListener) CaptureFrag2 (Fragment 2) DatePickerFragment

Thanks....

Community
  • 1
  • 1
seetha
  • 57
  • 7

1 Answers1

0

Does your activity implements

DatePickerDialog.OnDateSetListener

otherwise the cast

(DatePickerDialog.OnDateSetListener)getActivity()

will fail.

Edit:

Some sample code out of a fragment of mine:

private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year,
                                  int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                createFancyDateTimePicker(TIME_DIALOG_ID).show();
            }
        };


// Timepicker dialog generation
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHour = hourOfDay;
                mMinute = minute;
                updateTime();
            }


        };


protected Dialog createFancyDateTimePicker(int id) {
    switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(getActivity(),
                    mDateSetListener,
                    mYear, mMonth, mDay);

        case TIME_DIALOG_ID:
            return new TimePickerDialog(getActivity(),
                    mTimeSetListener, mHour, mMinute, false);

    }
    return null;
}

and if you want to show the dialog in your onclick method call:

startCalendar.setOnClickListener(new View.OnClickListener() {
       @Override
        public void onClick(View v) {
            PRESSED_CALENDAR = START_TIME;
            createFancyDateTimePicker(DATE_DIALOG_ID).show();
        }
    });


private void updateTime() {

            startTime.setText(
                    new StringBuilder()
                            // Month is 0 based so add 1
                            .append(mHour).append(" : ")
                            .append(mMinute).append(" , ")
                            .append(mDay).append(".")
                            .append(mMonth + 1).append(".")
                            .append(mYear).append(""));
}
loose11
  • 607
  • 6
  • 31
  • Yes. As you can see from the first block of code, I am casting it as you have mentioned. – seetha Nov 11 '14 at 12:19
  • Your fragment implements it, but how about your activity? – loose11 Nov 11 '14 at 13:34
  • Do you want me to shift the entire block of code that is there in CaptureFrag2 to MainActivity. Once I implement this OnDateSetListener, I need to implement onDataSet there too. What in your opinion should remain in CaptureFrag2? Thanks. – seetha Nov 11 '14 at 14:24
  • Go ahead an copy the entire block "onDataSet" to your Activity and implement DatePickerDialog.OnDateSetListener in the Activity. – loose11 Nov 11 '14 at 14:38
  • It works! Thanks. Two doubts to further refine them. – seetha Nov 11 '14 at 18:24
  • Thanks. But only problem is we will end up with the activity having this code which the fragment should ideally have. May be it is the way it is designed. Two doubts... 1. I have two date fields. Though I can put the same method in OnClick property, since both of them hits the same OnDateSet method, I can only assign the set date to the same button. Is there a way I can manipulate the view – seetha Nov 11 '14 at 19:11
  • Doubt 2. After assigning the date if I press the button again, I am shown the today'date since the DatePickerFragment fetches the system date. How can I achieve the button to display the system date for the first time and to display the last set date when otherwise. – seetha Nov 11 '14 at 19:11