Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Incovertible types; cannot cast android.preference.Preference to androidx.preference.SeekBarPreference

I am trying to retrieve the SeekBarPreference from the layout for my app's settings (preferences.xml). However, when I try to cast the findPreference("font_size") into a SeekBarPreference, I get the following error:

Inconvertible types; cannot cast android.preference.Preference to androidx.preference.SeekBarPreference

Below is my code corresponding to my settings page (MyPreferencesAcitivty.java)-the error occurs at this line: 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");

        }
    }

}

Below is the XML code for my settings page layout (preferences.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>

I have included the following implementation in my gradle (app-level):

implementation 'androidx.preference:preference:1.0.0'
like image 382
Adam Lee Avatar asked Mar 03 '20 18:03

Adam Lee


2 Answers

You're mixing to different systems of preferences. SeekBarPreference extends from androidx.preference.Preference while you use android.preference.Preference.

You have to use androidx.preference for all your preferences.

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

Also replace PreferenceActivity with AppCompatActivity and getFragmentManager() with getSupportFragmentManager().

like image 185
tynn Avatar answered Oct 27 '22 09:10

tynn


Inconvertible types; cannot cast android.preference.Preference to androidx.preference.SeekBarPreference

Casting a android.preference.Preference into androidx.preference.Preference is not proper and will get compile error as inconvertible types. Casting in Java is done within same hierarchy of types, that is between inherited types.

AndroidX replaces the original support library APIs with packages in the androidx namespace. Only the package and Maven artifact names changed; class, method, and field names did not change.

Problem coming from

import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.Preference;
import android.preference.EditTextPreference;

You should use

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

Your MyPreferencesActivity Activity will be

public class MyPreferencesActivity extends AppCompatActivity {

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

Your activity_pref.xml will be

<?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>

Then your MyPreferenceFragment will be

public class MyPreferenceFragment extends PreferenceFragmentCompat {

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

        final SeekBarPreference fontSizeSeekBar = (SeekBarPreference) findPreference("font_size");
    }
}
like image 29
IntelliJ Amiya Avatar answered Oct 27 '22 11:10

IntelliJ Amiya