[英]ListPreference: use string-array as Entry and integer-array as Entry Values doesn't work
我在settings.xml文件中使用ListPreference。 我想向用户显示3个选项列表。 当用户在“设置”中选择一个选项时,出现此错误:
java.lang.NullPointerException
at android.preference.ListPreference.onDialogClosed(ListPreference.java:264)
at android.preference.DialogPreference.onDismiss(DialogPreference.java:381)
at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1228)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
这是ListPreference的代码:
<ListPreference
android:entries="@array/date_alignement"
android:entryValues="@array/date_alignement_values"
android:key="settings_date_alignement"
android:summary="@string/settings_date_alignement_summary"
android:title="@string/settings_date_alignement_title" />
这是我用来填充条目的数组:
<string-array name="date_alignement">
<item>"Top"</item>
<item>"Center"</item>
<item>"Bottom"</item>
</string-array>
<integer-array name="date_alignement_values">
<item >0</item>
<item >1</item>
<item >2</item>
</integer-array>
在我的onSharedPreferenceChanged中,我通过以下方式使用这些值:
@Override
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
//Text
mAlignment = mPrefs.getInt(PREF_ALIGNMENT, 1);
switch (mAlignment) {
case 0:
offsetY = mHeight/3.0f;
break;
case 2:
offsetY = mHeight*2.0f/3.0f;
break;
default:
offsetY = mHeight/2.0f;
break;
}
}
如果我对entryValues使用另一个字符串数组,它将起作用。 例如,如果我使用相同的字符串数组作为值和条目:
android:entries="@array/date_alignement"
android:entryValues="@array/date_alignement"
然后我必须稍微修改一下代码,但它可以工作:
if(mAlignment.equalsIgnoreCase("center")) {
offsetY = mHeight/2.0f;
} else if(mAlignment.equalsIgnoreCase("top")) {
offsetY = mHeight/3.0f;
} else if(mAlignment.equalsIgnoreCase("bottom")) {
offsetY = mHeight*2.0f/3.0f;
}
为什么我不能对ListPreference条目和值使用字符串数组和整数数组?
答案很简单:因为Android是按这种方式设计的。 它仅将String
数组用于条目和条目值,仅此而已。 而且我看不到任何问题,因为您可以使用Integer.parseInt()
方法轻松地将String
转换为int
。 希望这可以帮助。
您可以将输入值转换为字符串,以使ListPreference
满意,然后在与持久数据存储区通信时将其转换为int
。
"1", "2", "3"
等 IntListPreference
,将值持久保存为int preferences.xml
文件中,将<ListPreference>
更改为<your.app.package.IntListPreference>
这是一个示例实现。 经过测试并在AndroidX Preference 1.0.0上工作。
public class IntListPreference extends ListPreference {
public IntListPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public IntListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public IntListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public IntListPreference(Context context) {
super(context);
}
@Override
protected boolean persistString(String value) {
int intValue = Integer.parseInt(value);
return persistInt(intValue);
}
@Override
protected String getPersistedString(String defaultReturnValue) {
int intValue;
if (defaultReturnValue != null) {
int intDefaultReturnValue = Integer.parseInt(defaultReturnValue);
intValue = getPersistedInt(intDefaultReturnValue);
} else {
// We haven't been given a default return value, but we need to specify one when retrieving the value
if (getPersistedInt(0) == getPersistedInt(1)) {
// The default value is being ignored, so we're good to go
intValue = getPersistedInt(0);
} else {
throw new IllegalArgumentException("Cannot get an int without a default return value");
}
}
return Integer.toString(intValue);
}
}
以下为我工作:
String objectName = prefs.getString("listPrefMelodyYd1", "");
switch (objectName.toUpperCase()) {
case "1":
playSound(catSound);
break;
case "2":
playSound(dogSound);
break;
case "3":
playSound(cowSound);
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.