Reputation: 85
I'm creating a PreferenceFragment
based on the developer guide but I get a NullPointerException
at the getDefaultSharedPreferences()
call in onCreate()
when I try to restore saved user preferences. I inflate the PreferenceFragment
from xml and I set default values too. The exception is thrown at getDefaultSharedPreferencesName()
within getDefaultSharedPreferences()
.
Here is the PreferenceFragment
class:
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>
Upvotes: 5
Views: 4013
Reputation: 152807
Context context = getActivity();
Your fragment is not associated with any activity when the object is instantiated and getActivity()
returns null
. Hence the NPE when trying to get preferences with a null Context
.
Postpone the getActivity()
to e.g. your onCreate()
(which is called after onAttach()
).
Upvotes: 6
Reputation: 157437
getActivity
returns untill onAttach
is called. Change
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context)
with
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getActivity())
Upvotes: 1