findPreference(java.lang.CharSequence) is deprecated

不羁岁月 提交于 2019-12-01 09:35:33

It is deprecated because Android moved to fragment-based activities. Calling findPreference(CharSequence) will still work in higher API levels. You are just encouraged to use fragments instead of a PreferenceActivity.

The reason for the deprecation can be found in the source:

This function is not relevant for a modern fragment-based PreferenceActivity.


In API 11+ you should use PreferenceFragment.


If you want to have your IDE ignore the error just add the following to your method: @SuppressWarnings("deprecation")

Arun Madhavan
public class SettingsActivity extends PreferenceActivity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Add 'general' preferences, defined in the XML file
    getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();

    // For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
    // updated when the preference changes.
}

public static class MyPreferenceFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener {
    @Override
    public void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_details);

        bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
    }

    private void bindPreferenceSummaryToValue(Preference preference) {
        // Set the listener to watch for value changes.
        preference.setOnPreferenceChangeListener(this);

        // Trigger the listener immediately with the preference's
        // current value.
        onPreferenceChange(preference,
                PreferenceManager
                        .getDefaultSharedPreferences(preference.getContext())
                        .getString(preference.getKey(), ""));
    }

    public boolean onPreferenceChange(Preference preference, Object value) {
        String stringValue = value.toString();
        if (preference instanceof ListPreference) {
            // For list preferences, look up the correct display value in
            // the preference's 'entries' list (since they have separate labels/values).
            ListPreference listPreference = (ListPreference) preference;
            int prefIndex = listPreference.findIndexOfValue(stringValue);
            if (prefIndex >= 0) {
                preference.setSummary(listPreference.getEntries()[prefIndex]);
            }
        } else {
            // For other preferences, set the summary to the value's simple string representation.
            preference.setSummary(stringValue);
            Log.v("onpreferencechange",stringValue);
        }
        return true;
    }
}

Instead of adding the methods

bindPreferenceSummaryToValue(Preference preference) &
onPreferenceChange(Preference preference, Object value)

inside the main SettingsActivity class create a fragment class which implements the Preference.OnPreferenceChangeListener add the both the methods inside it.

check out the code provided

hope it helps

Pihu

Well here is the solution:

API Level 11+ introduced PreferenceFragment as another way of constructing the contents of a PreferenceActivity. You are welcome to use them, but if you are still supporting older devices, you cannot use PreferenceFragment for those devices.

Try the following link: Non Deprecated findPreference() Method? - Android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!