可以为ListPreference执行此操作吗? (目标API 19)
<string-array name="alert_type">
<item>regular intervals</item>
<item>one time</item>
</string-array>
<integer-array name="alert_type_value">
<item>0</item>
<item>1</item>
</integer-array>
我收到此错误。当我将整数数组更改为字符串数组时,我会不得到以下错误。
05-10 20:54:52.179: W/dalvikvm(32634): threadid=1: thread exiting with uncaught exception (group=0x40bef1f8)
05-10 20:54:52.209: E/AndroidRuntime(32634): FATAL EXCEPTION: main
05-10 20:54:52.209: E/AndroidRuntime(32634): java.lang.NullPointerException
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.preference.ListPreference.findIndexOfValue(ListPreference.java:215)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.preference.ListPreference.getValueIndex(ListPreference.java:224)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.preference.ListPreference.getEntry(ListPreference.java:202)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.preference.ListPreference.getSummary(ListPreference.java:148)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.preference.Preference.onBindView(Preference.java:507)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.preference.Preference.getView(Preference.java:453)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:222)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.widget.AbsListView.obtainView(AbsListView.java:2222)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.widget.ListView.makeAndAddView(ListView.java:1774)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.widget.ListView.fillDown(ListView.java:672)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.widget.ListView.fillFromTop(ListView.java:732)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.widget.ListView.layoutChildren(ListView.java:1611)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.widget.AbsListView.onLayout(AbsListView.java:2052)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.view.View.layout(View.java:11484)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.view.ViewGroup.layout(ViewGroup.java:4229)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1644)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1502)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.view.View.layout(View.java:11484)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.view.ViewGroup.layout(ViewGroup.java:4229)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.view.View.layout(View.java:11484)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.view.ViewGroup.layout(ViewGroup.java:4229)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1644)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1502)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.view.View.layout(View.java:11484)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.view.ViewGroup.layout(ViewGroup.java:4229)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.view.View.layout(View.java:11484)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.view.ViewGroup.layout(ViewGroup.java:4229)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1671)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.os.Handler.dispatchMessage(Handler.java:99)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.os.Looper.loop(Looper.java:137)
05-10 20:54:52.209: E/AndroidRuntime(32634): at android.app.ActivityThread.main(ActivityThread.java:4511)
05-10 20:54:52.209: E/AndroidRuntime(32634): at java.lang.reflect.Method.invokeNative(Native Method)
05-10 20:54:52.209: E/AndroidRuntime(32634): at java.lang.reflect.Method.invoke(Method.java:511)
05-10 20:54:52.209: E/AndroidRuntime(32634): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
05-10 20:54:52.209: E/AndroidRuntime(32634): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
05-10 20:54:52.209: E/AndroidRuntime(32634): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
很遗憾,您无法使用普通ListPreference
执行此操作。但是,this blog post显示了如何创建自己的自定义列表首选项以接受整数数组,并且最后还有完整的源代码。
答案 1 :(得分:0)
对我来说最简单的方法是将字符串转换为整数并返回到侦听器中。
从string到int
intValue = Integer.parseInt(sharedPreferences.getString(ALERT_TYPE,"-1"));
alertTypePreference.setSummary(intValue == -1 ? "click to set" : (intValue == AlertTable.PERIODIC_TYPE ? "regular intervals" : "one time"));
并返回
alertTypePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (newValue.equals(Integer.toString(AlertTable.PERIODIC_TYPE))) {
setEnabledAlertType(AlertTable.PERIODIC_TYPE);
} else .....
return true;
}
});