【问题标题】:findPreference(java.lang.CharSequence) is deprecated不推荐使用 findPreference(java.lang.CharSequence)
【发布时间】:2015-01-15 04:17:05
【问题描述】:

它给了我错误“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;
}

}

【问题讨论】:

    标签: android android-debug


    【解决方案1】:

    它已被弃用,因为 Android 已转向基于片段的活动。调用 findPreference(CharSequence) 仍然可以在更高的 API 级别上工作。只是鼓励您使用片段而不是PreferenceActivity

    弃用原因可以在source:

    此函数与现代基于片段的 PreferenceActivity 无关。


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


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

    【讨论】:

      【解决方案2】:
      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 的片段类,在其中添加这两个方法。

      查看提供的代码

      希望对你有帮助

      【讨论】:

        【解决方案3】:

        这就是解决方案:

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

        试试下面的链接: Non Deprecated findPreference() Method? - Android

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-22
          • 2012-11-14
          • 2019-11-08
          • 2019-11-08
          • 2020-01-03
          • 2011-12-02
          相关资源
          最近更新 更多