我试图在Android中创建一个ListPreference。但是我没有在XML中创建所有内容,而是想在JAVA中添加Entries和EntriesValues。我有这个XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<ListPreference android:key="delay"
android:title="@string/settings_push_delay"
android:defaultValue="Default">
</ListPreference>
</PreferenceScreen>
然后我有了这个类,扩展了PreferenceActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
final ListPreference listPreference = (ListPreference) findPreference("delay");
listPreference.setOnPreferenceClickListener(this);
}
public boolean onPreferenceClick(Preference preference) {
ListPreference lp = (ListPreference)preference;
String[] array={"1","2","3"};
CharSequence[] entries = array;
CharSequence[] entryValues = array;
lp.setEntries(entries);
lp.setDefaultValue("1");
lp.setEntryValues(entryValues);
return true;
}
这只是我尝试做的测试,所以我完全可以理解如何动态创建偏好。
忘了说我在运行此代码时遇到异常:
04-14 00:47:10.432: E/AndroidRuntime(1330): FATAL EXCEPTION: main
04-14 00:47:10.432: E/AndroidRuntime(1330): java.lang.IllegalStateException: ListPreference requires an entries array and an entryValues array.
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.preference.ListPreference.onPrepareDialogBuilder(ListPreference.java:232)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.preference.DialogPreference.showDialog(DialogPreference.java:293)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.preference.DialogPreference.onClick(DialogPreference.java:264)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.preference.Preference.performClick(Preference.java:939)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:202)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.widget.AbsListView$1.run(AbsListView.java:3168)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.os.Handler.handleCallback(Handler.java:605)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.os.Looper.loop(Looper.java:137)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-14 00:47:10.432: E/AndroidRuntime(1330): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 00:47:10.432: E/AndroidRuntime(1330): at java.lang.reflect.Method.invoke(Method.java:511)
04-14 00:47:10.432: E/AndroidRuntime(1330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-14 00:47:10.432: E/AndroidRuntime(1330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-14 00:47:10.432: E/AndroidRuntime(1330): at dalvik.system.NativeStart.main(Native Method)
我的问题是,如何创建ListPreference而不在XML上定义Entries和EntryValues。怎么能在JAVA文件中做到这一点。那么,我如何解决这些异常?
谢谢,
答案 0 :(得分:5)
当没有entries
和entryValues
时,您无法在ListPreference
事件中设置onClick
的{{1}}和entries
从...开始。
而是在entryValues
。
onCreate
然后,如果您需要在onClick侦听器中动态设置值,它应该可以正常工作。