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....