onPreferenceChange出错

时间:2015-03-28 19:54:55

标签: android android-studio

这是我收到错误的文件,首选项中的getSummary()不能应用于preference.getSummary(listPreference.getEntries()...上的错误(java.lang.CharSequence),也是同一个getSummary的错误else语句中的()(java.lang.String)。

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

@Override
public void onCreate(Bundle savedInstanceState) {
    addPreferencesFromResource(R.xml.pref_general);
    bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
    bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_units_key)));
    super.onCreate(savedInstanceState);

}

private void bindPreferenceSummaryToValue(Preference preference){
    //set listener to watch for value change
    preference.setOnPreferenceChangeListener(this);

    //Trigger the listenter immediately with the preferences 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) {
        //look up the correct display value in the preferences enteries list
        ListPreference listPreference = (ListPreference) preference;
        int prefIndex = listPreference.findIndexOfValue(stringValue);
        if (prefIndex >= 0) {
            preference.getSummary(listPreference.getEntries()[prefIndex]);
        }

    }
    else{
            preference.getSummary(stringValue);
        }

        return true;
    }

}

1 个答案:

答案 0 :(得分:0)

getSummary()返回摘要,不带任何参数。

您最有可能寻找setSumary(CharSequence摘要)

http://developer.android.com/reference/android/preference/Preference.html#setSummary(java.lang.CharSequence)