java之Android:不可转换的类型;无法将 android.preference.Preference 转换为 androidx.preference.SeekBarPreference
dflying
阅读:59
2025-01-19 22:14:33
评论:0
我正在尝试从我的应用程序设置 (preferences.xml) 的布局中检索 SeekBarPreference。但是,当我尝试将 findPreference("font_size") 转换为 SeekBarPreference 时,出现以下错误:
Inconvertible types; cannot cast android.preference.Preference to androidx.preference.SeekBarPreference
下面是我的设置页面对应的代码(MyPreferencesAcitivty.java)——错误发生在这一行:
final SeekBarPreference fontSizeSeekBar = (SeekBarPreference) findPreference("font_size");
:
package com.example.myapplication;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.Preference;
import android.widget.EditText;
import androidx.preference.SeekBarPreference;
public class MyPreferencesActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment
{
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
// Retrieve the EditTextPreference demonstrating the font size
final EditTextPreference fontSizeEditPreference = (EditTextPreference) findPreference("font_size_edittext");
// Retrive the EditText component of the EditTextPreference
final EditText fontSizeEditText = fontSizeEditPreference.getEditText();
// Attach a listener to the font size seekbar (changes the size of the EditText)
final SeekBarPreference fontSizeSeekBar = (SeekBarPreference) findPreference("font_size");
}
}
}
以下是我的设置页面布局 (
preferences.xml
) 的 XML 代码:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.kizitonwose.colorpreference.ColorPreference
android:defaultValue="#FF8983"
android:title="Addition Highlight Color"
app:colorChoices="@array/highlight_colors"
android:key="addition_highlight_color"/>
<com.kizitonwose.colorpreference.ColorPreference
android:defaultValue="#99ffcc"
android:title="Removal Highlight Color"
app:colorChoices="@array/highlight_colors"
android:key="removal_highlight_color"/>
<SeekBarPreference
android:key="font_size"
android:title="Font Size"
android:min="12"
android:max="32"
android:defaultValue="14" />
<EditTextPreference
android:defaultValue="Default Value"
android:key="font_size_edittext"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="A" />
</PreferenceScreen>
我在我的 gradle(应用程序级)中包含了以下实现:
implementation 'androidx.preference:preference:1.0.0'
请您参考如下方法:
你正在混合不同的偏好系统。 SeekBarPreference
延伸自 androidx.preference.Preference
当您使用 android.preference.Preference
.
您必须使用 androidx.preference
满足您的所有喜好。
import androidx.preference.EditTextPreference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.Preference;
也替换
PreferenceActivity
与
AppCompatActivity
和
getFragmentManager()
与
getSupportFragmentManager()
.
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。