PreferenceFragment NullPointerException

时间:2015-05-16 19:22:55

标签: java android

public class MyPrefs extends PreferenceActivity {

    public static class MyPreferenceFragment extends PreferenceFragment
    {
        @Override
        public void onCreate(final Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
        }
    }

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

        MyPreferenceFragment frag =  new MyPreferenceFragment();

        getFragmentManager().beginTransaction().replace(android.R.id.content, frag).commit();

        Preference pref;
        pref = frag.findPreference("pref_gps_updates");

        pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                return true;
            }
        });

    }
}

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.preference.Preference.setOnPreferenceChangeListener(android.preference.Preference$OnPreferenceChangeListener)' on a null object reference

我不明白为什么我会遇到这次崩溃或我做错了什么?

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

    <EditTextPreference
        android:key="pref_gps_updates"
        android:inputType="number"
        android:title="@string/pref_gps_updates"
        android:summary="@string/pref_gps_updates_summary"
        android:defaultValue="60" />

</PreferenceScreen>    

4 个答案:

答案 0 :(得分:5)

Preference pref;
pref = frag.findPreference("pref_updates");

pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        return true;
    }
});

将此部分放入片段onCreate()(活动onCreate中 NOT )并仔细检查是否使用了密钥pref_updatesxml

更新

public class MyPrefs extends PreferenceActivity {

    public static class MyPreferenceFragment extends PreferenceFragment
    {
        @Override
        public void onCreate(final Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);


            Preference pref;
            pref = findPreference("pref_gps_updates");

            pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    return true;
                }
            });
        }
    }

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

        MyPreferenceFragment frag =  new MyPreferenceFragment();

        getFragmentManager().beginTransaction().replace(android.R.id.content, frag).commit();

    }
}

答案 1 :(得分:2)

不要为每个偏好设置创建PreferenceChangedListener,最好让PreferenceFragment实现OnSharedPreferenceChangeListener界面。

这样,无论您添加多少首选项,您都不需要为每个首选项添加新的侦听器,并且逻辑在@ {1}}中是自包含的。

这将是这样的:

PreferenceFragment

答案 2 :(得分:1)

在代码中使用

pref = frag.findPreference("pref_updates");

但在xml中它说

android:key="pref_gps_updates"

更改密钥需要匹配才能找到。

答案 3 :(得分:0)

在执行片段事务之前尝试并移动代码。

    MyPreferenceFragment frag =  new MyPreferenceFragment();

    Preference pref;
    pref = frag.findPreference("pref_gps_updates");

    pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            return true;
        }
    });

   getFragmentManager().beginTransaction().replace(android.R.id.content, frag).commit();

}

}