findPreference(java.lang.CharSequence中)已过时

时间:2015-01-15 04:17:06

标签: android android-debug

它给出了错误“findPreference(java.lang.CharSequence)已弃用”。目前,我的目标是针对我的应用程序的API 10及更高版本。任何有助于解决此问题的帮助都将受到赞赏。

公共类SettingsActivity扩展了PreferenceActivity         实现Preference.OnPreferenceChangeListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Add 'general' preferences, defined in the XML file
    // TODO: Add preferences from XML

    addPreferencesFromResource(R.xml.pref_general);

    bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key));
    // For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
    // updated when the preference changes.
    // TODO: Add preferences
}

/**
 * Attaches a listener so the summary is always updated with the preference value.
 * Also fires the listener once, to initialize the summary (so it shows up before the value
 * is changed.)
 */
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(), ""));
}

@Override
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);
    }
    return true;
}

}

3 个答案:

答案 0 :(得分:3)

它已被弃用,因为Android已移至基于片段的活动。调用findPreference(CharSequence)仍然可以在更高的API级别中运行。我们鼓励您使用片段而不是PreferenceActivity

可以在source

中找到弃用的原因
  

此功能与现代基于片段的PreferenceActivity无关。


在API 11+中,您应该使用PreferenceFragment


如果您想让IDE忽略该错误,只需将以下内容添加到您的方法中:@SuppressWarnings("deprecation")

答案 1 :(得分:3)

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;
    }
}

而不是添加方法

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

在主要的SettingsActivity类中创建一个实现Preference.OnPreferenceChangeListener的片段类,在其中添加两个方法。

查看提供的代码

希望有所帮助

答案 2 :(得分:1)

这是解决方案:

  

API Level 11+引入了PreferenceFragment作为另一种方式   构造PreferenceActivity的内容。不客气   使用它们,但如果您仍然支持旧设备,则不能   对这些设备使用PreferenceFragment。

请尝试以下链接: Non Deprecated findPreference() Method? - Android