title: Android PreferenceManager.setDefaultValues() crashes with class cast exception
body: <p>I have a frustrating problem, I have created a custom preference for Android, using the support library.</p>
  
  <pre><code>public class CustomTimePreference extends DialogPreference {
      public int hour = 0;
      public int minute = 0;
  
      public CustomTimePreference(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
  
      public static int parseHour(String value) {
          try {
              String[] time = value.split(":");
              return (Integer.parseInt(time[0]));
          } catch (Exception e) {
              return 0;
          }
      }
  
      public static int parseMinute(String value) {
          try {
              String[] time = value.split(":");
              return (Integer.parseInt(time[1]));
          } catch (Exception e) {
              return 0;
          }
      }
  
      public static String timeToString(int h, int m) {
          return String.format("%02d", h) + ":" + String.format("%02d", m);
      }
  
  
      @Override
      protected Object onGetDefaultValue(TypedArray a, int index) {
          return a.getString(index);
      }
  
      @Override
      protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
          String value;
          if (restoreValue) {
              if (defaultValue == null) value = getPersistedString("00:00");
              else value = getPersistedString(defaultValue.toString());
          } else {
              value = defaultValue.toString();
          }
  
          hour = parseHour(value);
          minute = parseMinute(value);
      }
  
  
      public void persistStringValue(String value) {
          persistString(value);
      }
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>public class CustomTimePreferenceDialogFragmentCompat extends PreferenceDialogFragmentCompat implements DialogPreference.TargetFragment {
      TimePicker timePicker = null;
  
      @Override
      protected View onCreateDialogView(Context context) {
          timePicker = new TimePicker(context);
          return (timePicker);
      }
  
      @Override
      protected void onBindDialogView(View v) {
          super.onBindDialogView(v);
          timePicker.setIs24HourView(true);
          CustomTimePreference pref = (CustomTimePreference) getPreference();
          timePicker.setCurrentHour(pref.hour);
          timePicker.setCurrentMinute(pref.minute);
      }
  
      @Override
      public void onDialogClosed(boolean positiveResult) {
          if (positiveResult) {
              CustomTimePreference pref = (CustomTimePreference) getPreference();
              pref.hour = timePicker.getCurrentHour();
              pref.minute = timePicker.getCurrentMinute();
  
              String value = CustomTimePreference.timeToString(pref.hour, pref.minute);
              if (pref.callChangeListener(value)) pref.persistStringValue(value);
          }
      }
  
      @Override
      public Preference findPreference(CharSequence charSequence) {
          return getPreference();
      }
  }
  </code></pre>
  
  <p>For completeness, the xml contained within the preferences.xml is:</p>
  
  <pre><code>&lt;customcontrols.CustomTimePreference
              android:key="time_pace_preference"
              android:title="@string/title_time_pace_preference"
              android:defaultValue="@string/settings_default_pace"
              android:summary="Set some time"
              /&gt;
  </code></pre>
  
  <p>However, when I attempt to call </p>
  
  <pre><code>PreferenceManager.setDefaultValues(mContext, preferences, true);
  </code></pre>
  
  <p>I receive</p>
  
  <pre><code>java.lang.ClassCastException: customcontrols.CustomTimePreference cannot be cast to android.preference.Preference
  </code></pre>
  
  <p>Why is this happening? as CustomTimePreference extends DialogPreference which itself extends Preference, this should be fine?!</p>
  
  <p>If I don't call the setDefaultValues() I am able to go into my settings fragment and view the custom control?</p>
  
  <p>What am I doing wrong, and how do I fix it!?</p>