问题描述
公共类我的preFS延长$ P $ {pferenceActivity 公共静态类我的preferenceFragment扩展preferenceFragment
{
@覆盖
公共无效的onCreate(最终捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
加preferencesFromResource(R.xml preferences);
}
} @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 我的preferenceFragment FRAG =新的我的preferenceFragment(); 。getFragmentManager()调用BeginTransaction()取代(android.R.id.content,FRAG).commit(); preference preF;
preF = frag.find preference(pref_gps_updates); pref.setOn preferenceChangeListener(新在preferenceChangeListener(){
@覆盖
在preferenceChange公共布尔(preference preference,对象为newValue){
返回true;
}
}); }
}
产生的原因:显示java.lang.NullPointerException:尝试调用虚拟方法无效android.$p$pference.$p$pference.setOn$p$pferenceChangeListener(android.$p$pference.$p$pference$On$p$pferenceChangeListener)'在一个空对象引用
我不明白为什么我得到这个崩溃或什么,我做错了什么?
<?XML版本=1.0编码=UTF-8&GT?;
< preferenceScreen的xmlns:机器人=http://schemas.android.com/apk/res/android> < EditText上preference
机器人:关键=pref_gps_updates
安卓的inputType =数字
机器人:标题=@字符串/ pref_gps_updates
机器人:总结=@字符串/ pref_gps_updates_summary
机器人:设置defaultValue =60/>< / preferenceScreen>
preference preF;
preF = frag.find preference(pref_updates);pref.setOn preferenceChangeListener(新在preferenceChangeListener(){
@覆盖
在preferenceChange公共布尔(preference preference,对象为newValue){
返回true;
}
});
将这个部分到你的碎片的的onCreate()
并仔细检查,如果该键用于 pref_updates
在你的 XML
。
更新:
公共类我的preFS延长$ P $ {pferenceActivity 公共静态类我的preferenceFragment扩展preferenceFragment
{
@覆盖
公共无效的onCreate(最终捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
加preferencesFromResource(R.xml preferences);
preference preF;
preF =找到preference(pref_gps_updates); pref.setOn preferenceChangeListener(新在preferenceChangeListener(){
@覆盖
在preferenceChange公共布尔(preference preference,对象为newValue){
返回true;
}
});
}
} @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 我的preferenceFragment FRAG =新的我的preferenceFragment(); 。getFragmentManager()调用BeginTransaction()取代(android.R.id.content,FRAG).commit(); }
}
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
I don't understand why I get this crash or what I am doing wrong?
<?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>
Preference pref;
pref = frag.findPreference("pref_updates");
pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
return true;
}
});
Put this part into your Fragment's onCreate()
and double check if the key pref_updates
is used in your xml
.
Update:
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();
}
}
这篇关于preferenceFragment NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!