我正在尝试在我的首选项 xml 中设置频率列表,但我不断收到此错误。我也附上了所有相关文件。我相信我设置了正确的值,但我找不到我的错误当前在哪里。另外,当我使用 SharedPreferences.getString(key, defaultValue) 返回什么值?ENTRY 还是 ENTRYVALUE?
我的错误:
08-20 00:14:25.195: E/AndroidRuntime(1260): FATAL EXCEPTION: main
08-20 00:14:25.195: E/AndroidRuntime(1260): java.lang.NullPointerException
08-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);
}
@Override
public 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");
}
}
}