简体   繁体   中英

How to fix cannot cast android.support.v7.preference.Preference to android.preference.RingtonePreference?

So i'm trying to implement this method to get the selected ringtone from a RingtonePreference but i'm getting this error at line

if (pref instanceof RingtonePreference)

I tried finding android.support.v7,preference.RingtonePreference but there was none in the existing packages.

should i import Gericop/Android-Support-Preference-V7-Fix from gitHub ?


 private String getRingtonePreferenceValue() {
        if (pref instanceof RingtonePreference) {

            Log.i("***", "RingtonePreference " + pref.getKey());
            final RingtonePreference ringPref = (RingtonePreference) pref;
            ringPref.setOnPreferenceChangeListener(new 
                      Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference,
                                                  Object newValue) {
                    Log.i("***", "Changed " + newValue.toString());
                    Ringtone ringtone = RingtoneManager.getRingtone(
                            getContext(), Uri.parse((String) newValue));
                    ringPref.setSummary(ringtone.getTitle(getContext()));
                    return true;
                }
            });

The android.preference.RingtonePreference is deprecated and there isn't a RingtonePreference in the support libraries or androidx libraries.

Google decided a few year ago to not move the class in support libraries.
In the same link there is a simple workaround, otherwise you have to create a custom Preference .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM