安卓:不可转换类型;无法将android.preference.preference强制转换为androidx.pref



我正试图从应用程序设置(preference.xml(的布局中检索SeekBarPreference。然而,当我试图将findPreference("font_size"(转换为SeekBarPreferences时,我会收到以下错误:

不可转换类型;无法将android.preference.preference强制转换为androidx.preferenceSeekBarPreference

下面是我的设置页面(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'

您正在混合使用不同的偏好系统。当您使用android.preference.Preference时,SeekBarPreferenceandroidx.preference.Preference扩展而来。

您必须对所有偏好使用androidx.preference

import androidx.preference.EditTextPreference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.Preference;

AppCompatActivity替换PreferenceActivity,用getSupportFragmentManager()替换getFragmentManager()

不可转换类型;无法将android.preference.preference强制转换为androidx.preferenceSeekBarPreference

android.preference.Preference强制转换为androidx.preference.Preference是不正确的,并且会由于不可转换类型而导致编译错误。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;

您的我的偏好活动活动将是

public class MyPreferencesActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pref);
}
}

您的活动_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");
}
}

最新更新