Android:隐蔽类型;无法将 android.preference.Preference 转换为 androidx.preference.SeekBarPreference

Ada*_*Lee 5 java android android-layout build.gradle

我正在尝试从我的应用程序设置 (preferences.xml) 的布局中检索 SeekBarPreference。但是,当我尝试将 findPreference("font_size") 转换为 SeekBarPreference 时,出现以下错误:

不可转换的类型;无法将 android.preference.Preference 转换为 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");

        }
    }

}
Run Code Online (Sandbox Code Playgroud)

下面是我的设置页面布局 ( 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>
Run Code Online (Sandbox Code Playgroud)

我在我的 gradle(应用级)中包含了以下实现:

implementation 'androidx.preference:preference:1.0.0'
Run Code Online (Sandbox Code Playgroud)

tyn*_*ynn 9

您正在混入不同的偏好系统。SeekBarPreference从androidx.preference.Preference您使用android.preference.Preference.

您必须根据androidx.preference自己的喜好使用。

import androidx.preference.EditTextPreference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.Preference;
Run Code Online (Sandbox Code Playgroud)

同时更换PreferenceActivity用AppCompatActivity和getFragmentManager()用getSupportFragmentManager()。