NullPointerException in getDefaultSharedPreferences

I create PreferenceFragmentbased on the developer's guide, but I get NullPointerExceptionwhen I call getDefaultSharedPreferences()in onCreate()when I try to restore the saved user settings. I inflate PreferenceFragmentfrom xml and I also set defaults. The exception is getDefaultSharedPreferencesName()inside getDefaultSharedPreferences().

Here is the class PreferenceFragment:

public class SettingsFragment extends PreferenceFragment implements
    OnSharedPreferenceChangeListener {

public static final String KEY_MAX_WALK_DISTANCE_PREFERENCE = "max_walk_distance_preference";
public static final String KEY_MAX_SEARCH_TIME_PREFERENCE = "max_search_time_preference";

Context context = getActivity();

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

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);

    // load user preferences
    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(context);
    if (sharedPreferences != null) {
        Preference walkDistance = findPreference(KEY_MAX_WALK_DISTANCE_PREFERENCE);
        if (walkDistance != null) {
            walkDistance.setSummary(sharedPreferences.getString(
                    KEY_MAX_WALK_DISTANCE_PREFERENCE, "")
                    + getResources().getString(
                            R.string.max_walk_distance_postfix));
        }
        Preference searchTime = findPreference(KEY_MAX_SEARCH_TIME_PREFERENCE);
        if (searchTime != null) {
            searchTime.setSummary(sharedPreferences.getString(
                    KEY_MAX_SEARCH_TIME_PREFERENCE, "")
                    + getResources().getString(
                            R.string.max_search_time_postfix));
        }
    }
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {
    if (key.equals(KEY_MAX_WALK_DISTANCE_PREFERENCE)) {
        Preference walkDistance = findPreference(key);
        // Set summary to be the user-description for the selected value
        walkDistance.setSummary(sharedPreferences.getString(key, "")
                + getResources().getString(
                        R.string.max_walk_distance_postfix));
    } else if (key.equals(KEY_MAX_SEARCH_TIME_PREFERENCE)) {
        Preference searchTime = findPreference(key);
        // Set summary to be the user-description for the selected value
        searchTime.setSummary(sharedPreferences.getString(key, "")
                + getResources()
                        .getString(R.string.max_search_time_postfix));
    }
}

@Override
public void onResume() {
    super.onResume();
    getPreferenceScreen().getSharedPreferences()
            .registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onPause() {
    super.onPause();
    getPreferenceScreen().getSharedPreferences()
            .unregisterOnSharedPreferenceChangeListener(this);
}

}

Here is the XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<EditTextPreference
    android:dialogTitle="@string/max_walk_distance_dialogtitle"
    android:key="max_walk_distance_preference"
    android:summary="@string/max_walk_distance_summary"
    android:title="@string/max_walk_distance_title"
    android:inputType="number"
    android:defaultValue="500" />

<EditTextPreference
    android:dialogTitle="@string/max_search_time_dialogtitle"
    android:key="max_search_time_preference"
    android:summary="@string/max_search_time_summary"
    android:title="@string/max_search_time_title"
    android:inputType="number"
    android:defaultValue="10"/>

</PreferenceScreen>
+4
source share
2 answers
Context context = getActivity();

Your fragment is not associated with any activity when creating the object and getActivity()returns null. Therefore, the NPE is trying to get preferences with a null value Context.

getActivity(), . onCreate() ( onAttach()).

+6

getActivity , onAttach.

 SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(context)

 SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(getActivity())
+1

Source: https://habr.com/ru/post/1536259/


All Articles