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

5
我正试图从我的应用程序设置(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");

        }
    }

}

以下是我的设置页面布局(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 (app-level)中:

implementation 'androidx.preference:preference:1.0.0'
2个回答

9
你混合了两个偏好设置系统。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()


4
无法将android.preference.Preference转换为androidx.preference.SeekBarPreference类型;无法转换类型会导致编译错误。在Java中,类型转换必须在相同类型层次结构之间进行,也就是在继承类型之间进行。 AndroidX 用androidx命名空间中的包替换了原始支持库API。只有包和Maven构件名称发生了更改;类、方法和字段名称未更改。
问题来源于:
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.Preference;
import android.preference.EditTextPreference;

您应该使用

import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.SeekBarPreference;
import androidx.preference.EditTextPreference;

你的MyPreferencesActivity活动将会

public class MyPreferencesActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pref);
    }
}

您的activity_pref.xml将会被:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <fragment
        android:id="@+id/preferenceFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name=".MyPreferenceFragment" />


</FrameLayout>

然后你的 MyPreferenceFragment 将会是:
public class MyPreferenceFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.preference);

        final SeekBarPreference fontSizeSeekBar = (SeekBarPreference) findPreference("font_size");
    }
}

我需要在main_activity.xml中包含activity_pref.xml吗? - Adam Lee
@AdamLee 不,你应该根据MyPreferencesActivity创建布局(activity_pref)。 - IntelliJ Amiya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接