gpt4 book ai didi

java - getDefaultSharedPreferences 处的 NullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:26:26 24 4
gpt4 key购买 nike

我正在根据开发人员指南创建一个 PreferenceFragment,但我在 中的 getDefaultSharedPreferences() 调用中得到了一个 NullPointerException onCreate() 当我尝试恢复保存的用户首选项时。我从 xml 中膨胀 PreferenceFragment 并且我也设置了默认值。异常在 getDefaultSharedPreferences() 中的 getDefaultSharedPreferencesName() 处抛出。

这是 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);
}

这是 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>

最佳答案

Context context = getActivity();

当对象被实例化并且 getActivity() 返回 null 时,您的 fragment 不与任何 Activity 相关联。因此,在尝试使用空 Context 获取首选项时会出现 NPE。

getActivity() 推迟到例如您的 onCreate()(在 onAttach() 之后调用)。

关于java - getDefaultSharedPreferences 处的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23035121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com