title: Android ClassCastException while implementing onTimeSet method of TimePickerDialog.OnTimeSetListener interface in another fragment
body: <p>I'm trying to implement <code>TimePickerDialog.OnTimeSetListener</code> in a fragment but encountering <code>ClassCastException</code> while doing so.</p>
  
  <p>Here's the code of <code>TimePickerFragment</code> class:</p>
  
  <pre><code>public class TimePickerFragment extends DialogFragment {
  
      @NonNull
      @Override
      public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
          final Calendar calendar = Calendar.getInstance();
          int hour = calendar.get(Calendar.HOUR_OF_DAY);
          int minute = calendar.get(Calendar.MINUTE);
  
          return new TimePickerDialog(
                  getActivity(),
                  (TimePickerDialog.OnTimeSetListener) getActivity(),
  //                this,
                  hour,
                  minute,
                  DateFormat.is24HourFormat(getActivity())
          );
      }
  }
  </code></pre>
  
  <p>I need to use the value of hour and minute selected by the user in another fragment named <code>SetAlarmFragment</code>. And so I've implemented <code>TimePickerDialog.OnTimeSetListener</code> in that fragment. Here's the code:</p>
  
  <pre><code>public class SetAlarmFragment extends Fragment implements TimePickerDialog.OnTimeSetListener {
  
      private TextView timeText;
      private Button timePickerButton;
      private Button confirmationButton;
  
      private int selectedHour;
      private int selectedMinute;
  
      @Nullable
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_set_alarm, container, false);
  
          initializeUiComponents(view);
          setClickEvents();
  
          final Calendar calendar = Calendar.getInstance();
          int hour = calendar.get(Calendar.HOUR_OF_DAY);
          int minute = calendar.get(Calendar.MINUTE);
  
          updateTimeText(hour, minute);
  
          return view;
      }
  
      @Override
      public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
          updateTimeText(hourOfDay, minute);
      }
  
      private void setClickEvents() {
          // to edit time
          timePickerButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  DialogFragment timePickerFragment = new TimePickerFragment();
                  timePickerFragment.show(getFragmentManager(), "timePicker");
              }
          });
  
          // on confirmation button pressed
          confirmationButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Calendar cal = Calendar.getInstance();
                  AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                  Intent intent = new Intent(getActivity(), AlarmReceiver.class);
                  PendingIntent alarmIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
                  if (Build.VERSION.SDK_INT &gt;= 19) {
                      alarmManager.setExact(
                              AlarmManager.RTC_WAKEUP,
                              cal.getTimeInMillis(),
                              alarmIntent
                      );
                  } else {
                      alarmManager.set(
                              AlarmManager.RTC_WAKEUP,
                              cal.getTimeInMillis(),
                              alarmIntent
                      );
                  }
              }
          });
      }
  
      private void initializeUiComponents(View view) {
          timeText = view.findViewById(R.id.time_text);
          timePickerButton = view.findViewById(R.id.set_time_button);
          confirmationButton = view.findViewById(R.id.confirm_button);
      }
  
  
      private void updateTimeText(int hour, int minute) {
          selectedHour = hour;
          selectedMinute = minute;
          timeText.setText(
                  String.format("%d:%d", selectedHour, selectedMinute)
          );
      }
  }
  </code></pre>
  
  <p>Now, I'm guessing that the reason behind this problem is I'm implementing the <code>onTimeSet()</code> method in a fragment, not an activity. But I need the value of hour and minute selected from TimePicker in SetAlarmFragment. Using an activity instead of fragment might solve this problem but I want to avoid it as far as I can. How can I solve this problem?</p>
  
  <p>Here's the error message:</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.apphousebd.digitalmcq.beta, PID: 4467
      java.lang.ClassCastException: com.apphousebd.digitalmcq.mainUi.MainActivity cannot be cast to android.app.TimePickerDialog$OnTimeSetListener
          at com.apphousebd.digitalmcq.fragments.TimePickerFragment.onCreateDialog(TimePickerFragment.java:26)
          at androidx.fragment.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:380)
          at androidx.fragment.app.Fragment.performGetLayoutInflater(Fragment.java:1412)
          at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
          at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
          at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
          at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
          at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
          at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
          at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
          at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
          at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
          at android.os.Handler.handleCallback(Handler.java:873)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:201)
          at android.app.ActivityThread.main(ActivityThread.java:6810)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
  E/UncaughtException: java.lang.ClassCastException: com.apphousebd.digitalmcq.mainUi.MainActivity cannot be cast to android.app.TimePickerDialog$OnTimeSetListener
          at com.apphousebd.digitalmcq.fragments.TimePickerFragment.onCreateDialog(TimePickerFragment.java:26)
          at androidx.fragment.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:380)
          at androidx.fragment.app.Fragment.performGetLayoutInflater(Fragment.java:1412)
          at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
          at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
          at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
          at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
          at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
          at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
          at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
          at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
          at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
          at android.os.Handler.handleCallback(Handler.java:873)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:201)
          at android.app.ActivityThread.main(ActivityThread.java:6810)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
  </code></pre>