gpt4 book ai didi

Android PreferenceManager.setDefaultValues() 因类转换异常而崩溃

转载 作者:行者123 更新时间:2023-11-30 01:18:41 26 4
gpt4 key购买 nike

我有一个令人沮丧的问题,我已经使用支持库为 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,而 DialogPreference 本身又扩展了 Preference,这应该没问题吧?!

如果我不调用 setDefaultValues(),我可以进入我的设置 fragment 并查看自定义控件吗?

我做错了什么,我该如何解决!?

最佳答案

如果您正在扩展 android.support.v7.preference.DialogPreference 将导致此崩溃。

如果是这样,您可以改用 android.support.v7.preference.PreferenceManager#setDefaultValues(android.content.Context, int, boolean)

关于Android PreferenceManager.setDefaultValues() 因类转换异常而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37433091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com