title: cannot be cast to android.app.DatePickerDialog$OnDateSetListener
body: <p>I unexpectedly encountered the following error upon button click in my application:</p>
  
  <pre><code> cannot be cast to android.app.DatePickerDialog$OnDateSetListener
  </code></pre>
  
  <p>What happens is as follow - user clicks a button called "btnCalendar" and then a calendar shows up</p>
  
  <pre><code>btnCalendar.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
  
          DialogP dcal = new  DialogP();
         dcal.show(ScheduleMatchOptionActivity.this.getFragmentManager(),"MyDialog");
      }
  });
  </code></pre>
  
  <p>Below is the DialogP class</p>
  
  <pre><code>public class DialogP extends DialogFragment {
  
      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
          DatePickerDialog dialog = new DatePickerDialog(getActivity(), (OnDateSetListener) this, year, month,day);
          Log.i("onCreateDialog", "onCreateDialog : MinDate() : "+new Date().getTime());
  
          Calendar cal=new GregorianCalendar();
  
          //min date
          dialog.getDatePicker().setMinDate(cal.getTime().getTime()-1000);
          Log.i("onCreateDialog", "onCreateDialog : MinDate() : "+cal.getTime());
  
          //max date .... added 30 days from currunt date
          cal.add(Calendar.DATE, 30);
          Date dte =cal.getTime();
          dialog.getDatePicker().setMaxDate(dte.getTime());
          Log.i("onCreateDialog", "onCreateDialog : MaxDate() : "+dte);
  
          //customizing calendar view
          dialog.getDatePicker().setCalendarViewShown(true);
          dialog.getDatePicker().setSpinnersShown(false);
          dialog.getDatePicker().getCalendarView().setShowWeekNumber(false);
          dialog.setCancelable(true);
  
          Log.i("Date", "date : "+System.currentTimeMillis());
          return dialog;
      }
  }
  </code></pre>
  
  <p>Below is the log cat message</p>
  
  <pre><code> 09-30 16:12:00.805: E/AndroidRuntime(1415): FATAL EXCEPTION: main
      09-30 16:12:00.805: E/AndroidRuntime(1415): java.lang.ClassCastException: com.dooba.beta.DialogP cannot be cast to android.app.DatePickerDialog$OnDateSetListener
      09-30 16:12:00.805: E/AndroidRuntime(1415):     at com.dooba.beta.DialogP.onCreateDialog(DialogP.java:25)
      09-30 16:12:00.805: E/AndroidRuntime(1415):     at android.app.DialogFragment.getLayoutInflater(DialogFragment.java:398)
      09-30 16:12:00.805: E/AndroidRuntime(1415):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
      09-30 16:12:00.805: E/AndroidRuntime(1415):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
      09-30 16:12:00.805: E/AndroidRuntime(1415):     at android.app.BackStackRecord.run(BackStackRecord.java:682)
      09-30 16:12:00.805: E/AndroidRuntime(1415):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
      09-30 16:12:00.805: E/AndroidRuntime(1415):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
      09-30 16:12:00.805: E/AndroidRuntime(1415):     at android.os.Handler.handleCallback(Handler.java:730)
      09-30 16:12:00.805: E/AndroidRuntime(1415):     at android.os.Handler.dispatchMessage(Handler.java:92)
      09-30 16:12:00.805: E/AndroidRuntime(1415):     at android.os.Looper.loop(Looper.java:137)
      09-30 16:12:00.805: E/AndroidRuntime(1415):     at android.app.ActivityThread.main(ActivityThread.java:5103)
      09-30 16:12:00.805: E/AndroidRuntime(1415):     at java.lang.reflect.Method.invokeNative(Native Method)
      09-30 16:12:00.805: E/AndroidRuntime(1415):     at java.lang.reflect.Method.invoke(Method.java:525)
      09-30 16:12:00.805: E/AndroidRuntime(1415):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
      09-30 16:12:00.805: E/AndroidRuntime(1415):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
      09-30 16:12:00.805: E/AndroidRuntime(1415):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>line 25 is the following:</p>
  
  <pre><code>DatePickerDialog dialog = new DatePickerDialog(getActivity(), (OnDateSetListener) this, year, month,day);
  </code></pre>
  
  <p>Before I had at        </p>
  
  <pre><code> DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month,day); 
  </code></pre>
  
  <p>but was getting the following error "The constructor DatePickerDialog(Activity, DialogP, int, int, int) is undefined" so I cast it.</p>
  
  <p>Another related question I would like to retrieve the date set and cast it unto a textview called txtDate that is displayed to the user. I am familiar with settext but a bit confused.</p>
  
  <p>Any help would be greatly appreciated. Thanks in advance</p>
  
  <p>Update:</p>
  
  <p>cal.add(Calendar.DATE, 7);
  if the number is less than 12 than the follow error occurs:</p>
  
  <pre><code>ALL i want to do is to limit the calendar to one week
      09-30 16:39:07.217: E/AndroidRuntime(2410): FATAL EXCEPTION: main
      09-30 16:39:07.217: E/AndroidRuntime(2410): java.lang.NullPointerException
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.CalendarView.onScroll(CalendarView.java:1214)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.CalendarView.access$900(CalendarView.java:76)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.CalendarView$2.onScroll(CalendarView.java:1075)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1340)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.ListView.layoutChildren(ListView.java:1762)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.AbsListView.onLayout(AbsListView.java:2012)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.View.layout(View.java:14289)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.View.layout(View.java:14289)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.View.layout(View.java:14289)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.View.layout(View.java:14289)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.View.layout(View.java:14289)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.View.layout(View.java:14289)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.View.layout(View.java:14289)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.View.layout(View.java:14289)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.View.layout(View.java:14289)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.View.layout(View.java:14289)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.View.layout(View.java:14289)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.Choreographer.doFrame(Choreographer.java:532)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.os.Handler.dispatchMessage(Handler.java:92)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.os.Looper.loop(Looper.java:137)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at android.app.ActivityThread.main(ActivityThread.java:5103)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at java.lang.reflect.Method.invokeNative(Native Method)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at java.lang.reflect.Method.invoke(Method.java:525)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
      09-30 16:39:07.217: E/AndroidRuntime(2410):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>