当我更改共享首选项中的值应用程序崩溃

时间:2016-08-23 06:10:13

标签: android sharedpreferences

http://i.stack.imgur.com/vnfQt.png

如上所示从我将温度单位的值从公制更改为英制并按下后退按钮时的设置我的应用程序崩溃并显示以下错误

http://i.stack.imgur.com/dBeHk.png

SettingsActivity

    package com.example.poo.sunshine;

    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.preference.ListPreference;
    import android.preference.Preference;
    import android.preference.PreferenceFragment;
    import android.preference.PreferenceManager;

    public class SettingsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(R.id.container, new MyPreferenceFragment())
                .commit();
    }

    public static class MyPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Add 'general' preferences, defined in the XML file
            addPreferencesFromResource(R.xml.pref_general);

            // For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
            // updated when the preference changes.
            PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(this);

        }

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            Preference locationPref = findPreference(getString(R.string.pref_location_key));
            Preference unitsPref = findPreference(getString(R.string.pref_units_key));
            prefChanged(sharedPreferences, locationPref, key);
            prefChanged(sharedPreferences, unitsPref, key);
        }

        private void prefChanged(SharedPreferences sharedPreferences, Preference pref, String key) {
            if (sharedPreferences 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) sharedPreferences;
                int prefIndex = listPreference.findIndexOfValue(key);
                if (prefIndex >= 0) {
                    pref.setSummary(listPreference.getEntries()[prefIndex]);
                } else {
                    pref.setSummary(sharedPreferences.getString(key, ""));
                }    
            }    
        }
    }
}

1 个答案:

答案 0 :(得分:0)

问题是因为您的Fragment尚未附加到您的SettingActivity视图。

要避免此错误,您可以在片段中访问视图之前查看isAdded()

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if(isAdded()){
            Preference locationPref = findPreference(getString(R.string.pref_location_key));
            Preference unitsPref = findPreference(getString(R.string.pref_units_key));
            prefChanged(sharedPreferences, locationPref, key);
            prefChanged(sharedPreferences, unitsPref, key);
        }

}

另一种解决方案,我也想提出建议。 您应该在片段中的onViewCreated()中注册首选项更改的侦听器。这意味着你的片段完全依附。

 @Override
 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        PreferenceManager.getDefaultSharedPreferences(getActivity())
        .registerOnSharedPreferenceChangeListener(this);
 }

希望这会奏效!