我正在尝试在我的首选项xml中设置频率列表,但我一直收到此错误.我也附上了所有相关文件.我相信我正确设置了值,但我找不到我的错误当前的位置.另外,当我使用SharedPreferences.getString(key,defaultValue)时返回什么值? ENTRY或ENTRYVALUE?
我的错误:
08-20 00:14:25.195: E/AndroIDRuntime(1260): FATAL EXCEPTION: main08-20 00:14:25.195: E/AndroIDRuntime(1260): java.lang.NullPointerException08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.preference.ListPreference.findindexOfValue(ListPreference.java:215)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.preference.ListPreference.getValueIndex(ListPreference.java:224)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.preference.ListPreference.getEntry(ListPreference.java:202)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.preference.ListPreference.getSummary(ListPreference.java:148)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.preference.Preference.onBindVIEw(Preference.java:515)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.preference.Preference.getVIEw(Preference.java:453)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.preference.PreferenceGroupAdapter.getVIEw(PreferenceGroupAdapter.java:222)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.Widget.AbsListVIEw.obtainVIEw(AbsListVIEw.java:2461)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.Widget.ListVIEw.makeAndAddVIEw(ListVIEw.java:1775)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.Widget.ListVIEw.fillDown(ListVIEw.java:678)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.Widget.ListVIEw.fillFromtop(ListVIEw.java:739)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.Widget.ListVIEw.layoutChildren(ListVIEw.java:1628)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.Widget.AbsListVIEw.onLayout(AbsListVIEw.java:2296)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.vIEw.VIEw.layout(VIEw.java:14063)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4603)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.Widget.linearLayout.setChildFrame(linearLayout.java:1655)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.Widget.linearLayout.layoutVertical(linearLayout.java:1513)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.Widget.linearLayout.onLayout(linearLayout.java:1426)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.vIEw.VIEw.layout(VIEw.java:14063)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4603)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.Widget.FrameLayout.onLayout(FrameLayout.java:448)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.vIEw.VIEw.layout(VIEw.java:14063)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4603)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.Widget.linearLayout.setChildFrame(linearLayout.java:1655)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.Widget.linearLayout.layoutVertical(linearLayout.java:1513)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.Widget.linearLayout.onLayout(linearLayout.java:1426)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.vIEw.VIEw.layout(VIEw.java:14063)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4603)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.Widget.FrameLayout.onLayout(FrameLayout.java:448)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.vIEw.VIEw.layout(VIEw.java:14063)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4603)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.vIEw.VIEwRootImpl.performlayout(VIEwRootImpl.java:1994)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.vIEw.VIEwRootImpl.performTraversals(VIEwRootImpl.java:1815)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.vIEw.VIEwRootImpl.doTraversal(VIEwRootImpl.java:1112)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.vIEw.VIEwRootImpl$TraversalRunnable.run(VIEwRootImpl.java:4518)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.vIEw.Choreographer$CallbackRecord.run(Choreographer.java:725)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.vIEw.Choreographer.doCallbacks(Choreographer.java:555)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.vIEw.Choreographer.doFrame(Choreographer.java:525)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.vIEw.Choreographer$FramedisplayEventReceiver.run(Choreographer.java:711)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.os.Handler.handleCallback(Handler.java:615)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.os.Handler.dispatchMessage(Handler.java:92)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.os.Looper.loop(Looper.java:137)08-20 00:14:25.195: E/AndroIDRuntime(1260): at androID.app.ActivityThread.main(ActivityThread.java:4898)08-20 00:14:25.195: E/AndroIDRuntime(1260): at java.lang.reflect.Method.invokeNative(Native Method)08-20 00:14:25.195: E/AndroIDRuntime(1260): at java.lang.reflect.Method.invoke(Method.java:511)08-20 00:14:25.195: E/AndroIDRuntime(1260): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)08-20 00:14:25.195: E/AndroIDRuntime(1260): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:773)08-20 00:14:25.195: E/AndroIDRuntime(1260): at dalvik.system.NativeStart.main(Native Method)
xml布局:
<ListPreference androID:key="frequency_key" androID:title="Sample Rate" androID:defaultValue="8000" androID:entrIEs="@array/freq_Titles" androID:entryValues="@array/freq_values" />
xml数组:
<?xml version="1.0" enCoding="utf-8"?><resources><string-array name="freq_Titles"> <item name="8000">8k Hz</item> <item name="16000">16k Hz</item> <item name="22050">22.05k Hz</item> <item name="44100">44.1k Hz</item> <item name="48000">48k Hz</item></string-array><array name="freq_values"> <item name="8000">8000</item> <item name="16000">16000</item> <item name="22050">22050</item> <item name="44100">44100</item> <item name="48000">48000</item></array></resources>
更新:即使我从默认的共享首选项中执行getAll(),它也很奇怪,它向我显示“frequency_key = 8k Hz”.所以它已经初始化了.
来自我的偏好类的更多代码:
public class Settings_ActivityAH extends PreferenceFragment implements OnSharedPreferencechangelistener{private SharedPreferences prefs;private Preference myAccountname;private Preference myGoogleUsername;private Preference myGooglePassword;private static Context context;public voID onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); addPreferencesFromresource(R.xml.preferences); context = getActivity(); if (context == null){ Log.e("error","context is null"); } prefs = PreferenceManager.getDefaultSharedPreferences(context); myAccountname = (Preference) findPreference("account_name_Title_key"); myGoogleUsername = (Preference) findPreference("Googleusername_key"); myGooglePassword = (Preference) findPreference("Googlepassword_key"); myAccountname.setSummary(prefs.getString("account_name_Title_key", "none")); myGoogleUsername.setTitle("username: "+prefs.getString("Googleusername_key", "none")); myGooglePassword.setTitle("password: "+prefs.getString("Googlepassword_key", "none")); Log.e("Check prefs",prefs.getAll().toString()); prefs.registerOnSharedPreferencechangelistener(this);}@OverrIDepublic voID onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Log.e("in onSharedPref","yea"); Preference preference = findPreference(key); if (preference.getClass() == EditTextPreference.class){ Log.e("in editText Pref", "yeah"); if( key.equals("account_name_Title_key")){ myAccountname.setSummary(sharedPreferences.getString("account_name_key","none")); Log.e("accountname prefs",sharedPreferences.getString("account_name_key","none")); } if( key.equals("account_name_key")){ myAccountname.setSummary(sharedPreferences.getString("account_name_key","none")); Log.e("accountname prefs",sharedPreferences.getString("account_name_key","none")); } else if (preference.getClass() == Preferencecategory.class){ Log.e("in category Pref", "yeah"); if (key.equals("Googleusername_key")){ myGoogleUsername.setTitle("Username: "+sharedPreferences.getString(key, "none")); Log.e("Googleusername prefs",sharedPreferences.getString(key, "none")); } else if( key.equals("Googlepassword_key")){ myGooglePassword.setTitle("Password: "+sharedPreferences.getString(key, "none")); Log.e("Googlepassword prefs",sharedPreferences.getString(key, "none")); } }}else{ Log.e("insufficIEnt key called","onSharedPreferenceChanged AH");}}
}
解决方法:
似乎AndroID只接受字符串数组作为ListPreference XML属性androID:entryValues的参数.这与所有XML属性仅采用具有一种特定的预定类型(即int,bool,string等)的参数一致.请参阅以下问题:ListPreference: use string-array as Entry and integer-array as Entry Values doesn’t work
关于你的问题,getString()将返回与被点击的条目对应的androID:entryValues数组的元素. androID:entrIEs数组仅用作用户的一系列显示字符串,并且在代码中没有其他影响,而androID:entryValues中的值用于应用程序中的持久存储.
总结以上是内存溢出为你收集整理的android – SharedPreferences ListPreference NullPointerException全部内容,希望文章能够帮你解决android – SharedPreferences ListPreference NullPointerException所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)