PointerException异常在getDefaultSha

PointerException异常在getDefaultSha

本文介绍了NullPointerException异常在getDefaultShared preferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个 preferenceFragment 基础上,开发人员指南,但我在获得 NullPointerException异常的onCreate getDefaultShared preferences()()调用当我试图恢复已保存的用户preferences。我从XML充气 preferenceFragment 我设置默认值了。唯一的例外是在 getDefaultShared preferencesName抛出() getDefaultShared preferences()

下面是 preferenceFragment 类:

 公共类SettingsFragment扩展preferenceFragment工具
    OnShared $ P $ {pferenceChangeListener公共静态最后弦乐KEY_MAX_WALK_DISTANCE_ preFERENCE =max_walk_distance_ preference
公共静态最后弦乐KEY_MAX_SEARCH_TIME_ preFERENCE =max_search_time_ preference上下文的背景下= getActivity();@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    //从XML资源负载的preferences
    加preferencesFromResource(R.xml preferences);    //加载用户preferences
    共享preferences共享preferences = preferenceManager
            .getDefaultShared preferences(背景);
    如果(共享preferences!= NULL){
        preference walkDistance =寻找preference(KEY_MAX_WALK_DISTANCE_ preFERENCE);
        如果(walkDistance!= NULL){
            walkDistance.setSummary(共享preferences.getString(
                    KEY_MAX_WALK_DISTANCE_ preFERENCE,)
                    + getResources()。的getString(
                            R.string.max_walk_distance_postfix));
        }
        preference searchTime =找到preference(KEY_MAX_SEARCH_TIME_ preFERENCE);
        如果(searchTime!= NULL){
            sea​​rchTime.setSummary(共享preferences.getString(
                    KEY_MAX_SEARCH_TIME_ preFERENCE,)
                    + getResources()。的getString(
                            R.string.max_search_time_postfix));
        }
    }
}@覆盖
公共无效onShared preferenceChanged(共享preferences共享preferences,
        字符串键){
    如果(key.equals(KEY_MAX_WALK_DISTANCE_ preFERENCE)){
        preference walkDistance =寻找preference(键);
        //设置总结为用户描述所选值
        walkDistance.setSummary(共享preferences.getString(键,)
                + getResources()。的getString(
                        R.string.max_walk_distance_postfix));
    }否则如果(key.equals(KEY_MAX_SEARCH_TIME_ preFERENCE)){
        preference searchTime =找到preference(键);
        //设置总结为用户描述所选值
        sea​​rchTime.setSummary(共享preferences.getString(键,)
                + getResources()
                        .getString(R.string.max_search_time_postfix));
    }
}@覆盖
公共无效onResume(){
    super.onResume();
    GET preferenceScreen()。getShared preferences()
            .registerOnShared preferenceChangeListener(本);
}@覆盖
公共无效的onPause(){
    super.onPause();
    GET preferenceScreen()。getShared preferences()
            .unregisterOnShared preferenceChangeListener(本);
}

}

下面是XML:

 <?XML版本=1.0编码=UTF-8&GT?;
< preferenceScreen的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>< EditText上preference
    机器人:DIALOGTITLE =@字符串/ max_walk_distance_dialogtitle
    机器人:键=max_walk_distance_ preference
    机器人:总结=@字符串/ max_walk_distance_summary
    机器人:标题=@字符串/ max_walk_distance_title
    安卓的inputType =数字
    机器人:设置defaultValue =500/>< EditText上preference
    机器人:DIALOGTITLE =@字符串/ max_search_time_dialogtitle
    机器人:键=max_search_time_ preference
    机器人:总结=@字符串/ max_search_time_summary
    机器人:标题=@字符串/ max_search_time_title
    安卓的inputType =数字
    机器人:设置defaultValue =10/>< / preferenceScreen>


解决方案

 上下文的背景下= getActivity();

当对象实例化和 getActivity()收益您的片段不与任何活动相关。因此,试图获得preferences用一个空当NPE 上下文

推迟 getActivity()来如你的的onCreate()(这是后 onAttach称为())。

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>
解决方案
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()).

这篇关于NullPointerException异常在getDefaultShared preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 18:43