San*_*rab 69 android listpreference
我在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)
Run Code Online (Sandbox Code Playgroud)
这是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" />
Run Code Online (Sandbox Code Playgroud)
以下是我用来填充条目的数组:
<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>
Run Code Online (Sandbox Code Playgroud)
在我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我为entryValues使用另一个字符串数组,它可以工作.例如,如果我使用相同的字符串数组作为值和条目:
android:entries="@array/date_alignement"
android:entryValues="@array/date_alignement"
Run Code Online (Sandbox Code Playgroud)
然后我必须稍微改变我的代码,但它的工作原理:
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;
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能对ListPreference条目和值使用字符串数组和整数数组?
Ego*_*gor 83
答案很简单:因为Android是这样设计的.它只是String
为条目和条目值使用数组,这就是全部.而且我在这个事实中看不出任何问题,因为您可以轻松地将a转换String
为int
使用该Integer.parseInt()
方法.希望这可以帮助.
您可以将条目值转换为字符串以保持ListPreference
满意,然后int
在与持久数据存储通信时将它们转换为 s。
"1", "2", "3"
等IntListPreference
以将值保留为整数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);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
41143 次 |
最近记录: |