我遇到了令人沮丧的问题,我使用支持库为Android创建了自定义首选项。
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);
}
}
和
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();
}
}
为完整起见,preferences.xml中包含的xml为:
<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"
/>
然而,当我试图打电话
时PreferenceManager.setDefaultValues(mContext, preferences, true);
我收到了
java.lang.ClassCastException: customcontrols.CustomTimePreference cannot be cast to android.preference.Preference
为什么会这样?由于CustomTimePreference扩展了DialogPreference,它本身扩展了Preference,这应该没问题?!
如果我没有调用setDefaultValues(),我可以进入我的设置片段并查看自定义控件吗?
我做错了什么,我该如何解决它??
答案 0 :(得分:1)
如果您要延长导致此次崩溃的android.support.v7.preference.DialogPreference
。
如果是这样,您可以改为使用android.support.v7.preference.PreferenceManager#setDefaultValues(android.content.Context, int, boolean)
。