title: Implement a DatePicker inside a Fragment
body: <p>I have below codes to display a date picker inside an Activity, but when I tried to convert my activity to Fragment, I am getting error. I can't really see any difference in code compare to solutions in SO to implement a DialogFragment inside a Fragment.</p>
  
  <p>Can you please point out what am I missing?</p>
  
  <p><strong>MainFragment:</strong></p>
  
  <pre><code>public class AddEntryFragment extends Fragment implements DatePickerFragment.TheListener{
      EditText datpurchased;
      public AddEntryFragment(){}
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.activity_addentry, container, false);
          return rootView;
      }
  
      @Override
      public void onStart() {
          super.onStart();
          datpurchased = (EditText) getView().findViewById(R.id.datepurchased);
          datpurchased.setOnClickListener(onClickListener);
      }
  
      private OnClickListener onClickListener = new OnClickListener() {
          @Override
          public void onClick(View v) {
              switch(v.getId()){
                  case R.id.datepurchased:
                      showdate(v);
                  break;
                  default:
                  break;
              }
  
        }
      };
  
      public void showdate(View v) {
          DialogFragment newFragment = new DatePickerFragment();
          Bundle bundle = new Bundle();
          bundle.putString("dateAsText",datpurchased.getText().toString());
          newFragment.setArguments(bundle); 
          newFragment.show(getFragmentManager(), "datePicker");
      }
  
      public void returnDate(String date) {
          datpurchased.setText(date);
      }
  }
  </code></pre>
  
  <p><strong>DatePickerFragment:</strong></p>
  
  <pre><code>public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
      TheListener listener;
  
      public interface TheListener{
          public void returnDate(String date);
      }
  
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState){
          Bundle bundle = getArguments();
          String date = bundle.getString("dateAsText");
          int year, month, day;
          SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
          df.setLenient(false);
          Date d = null;
          Calendar c=Calendar.getInstance();
          try {
              d = df.parse(date);
              c.setTime(d);
          } catch (ParseException e) {
              //e.printStackTrace();
          }
          year=c.get(Calendar.YEAR);
          month=c.get(Calendar.MONTH);
          day=c.get(Calendar.DAY_OF_MONTH);
          listener = (TheListener) getActivity();
          return new DatePickerDialog(getActivity(), this, year, month, day);
      }
  
      public void onDateSet(DatePicker view, int year, int month, int day){
          Calendar c = Calendar.getInstance();
          c.set(year, month, day);
  
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
          String formattedDate = sdf.format(c.getTime());
          if (listener != null) 
          {
            listener.returnDate(formattedDate); 
  
          }
      }
  }
  </code></pre>
  
  <p><strong>LogCat:</strong></p>
  
  <pre><code>12-18 23:35:41.094: D/AndroidRuntime(1058): Shutting down VM
  12-18 23:35:41.094: W/dalvikvm(1058): threadid=1: thread exiting with uncaught exception (group=0xb4a65b90)
  12-18 23:35:41.124: E/AndroidRuntime(1058): FATAL EXCEPTION: main
  12-18 23:35:41.124: E/AndroidRuntime(1058): Process: com.migrationdesk.mylibman, PID: 1058
  12-18 23:35:41.124: E/AndroidRuntime(1058): java.lang.ClassCastException: com.migrationdesk.mylibman.NavContainer cannot be cast to com.migrationdesk.mylibman.DatePickerFragment$TheListener
  12-18 23:35:41.124: E/AndroidRuntime(1058):     at com.migrationdesk.mylibman.DatePickerFragment.onCreateDialog(DatePickerFragment.java:41)
  12-18 23:35:41.124: E/AndroidRuntime(1058):     at android.app.DialogFragment.getLayoutInflater(DialogFragment.java:398)
  12-18 23:35:41.124: E/AndroidRuntime(1058):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
  12-18 23:35:41.124: E/AndroidRuntime(1058):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
  12-18 23:35:41.124: E/AndroidRuntime(1058):     at android.app.BackStackRecord.run(BackStackRecord.java:684)
  12-18 23:35:41.124: E/AndroidRuntime(1058):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
  12-18 23:35:41.124: E/AndroidRuntime(1058):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
  12-18 23:35:41.124: E/AndroidRuntime(1058):     at android.os.Handler.handleCallback(Handler.java:733)
  12-18 23:35:41.124: E/AndroidRuntime(1058):     at android.os.Handler.dispatchMessage(Handler.java:95)
  12-18 23:35:41.124: E/AndroidRuntime(1058):     at android.os.Looper.loop(Looper.java:137)
  12-18 23:35:41.124: E/AndroidRuntime(1058):     at android.app.ActivityThread.main(ActivityThread.java:4998)
  12-18 23:35:41.124: E/AndroidRuntime(1058):     at java.lang.reflect.Method.invokeNative(Native Method)
  12-18 23:35:41.124: E/AndroidRuntime(1058):     at java.lang.reflect.Method.invoke(Method.java:515)
  12-18 23:35:41.124: E/AndroidRuntime(1058):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
  12-18 23:35:41.124: E/AndroidRuntime(1058):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
  12-18 23:35:41.124: E/AndroidRuntime(1058):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>