我在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; …
Run Code Online (Sandbox Code Playgroud) 我需要在Activity启动时为ListPreference设置defult值.我已尝试过,ListPreference.setDefaultvalue("value");
但它会将列表的第一个条目作为默认值.我需要它,因为我必须检查一个条件并设置为默认值满足该条件的值,所以我认为它不能从xml文件中完成(带android:defaultValue
)
例如,假设我在arrays.xml中有这个值数组:
<string-array name="opts">
<item>red</item>
<item>green</item>
<item>blue</item>
</string-array>
<string-array name="opts_values">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
Run Code Online (Sandbox Code Playgroud)
在PreferenceScreen xml中:
<ListPreference
android:title="Colour select"
android:summary="Select your favourite"
android:key="colour"
android:entries="@array/opts"
android:entryValues="@array/opts_values" />
Run Code Online (Sandbox Code Playgroud)
在活动中,我想做这样的事情:
String mycolour;
if (something) {
mycolour="1";
} else {
mycolour="2";
}
ListPreference colour = (ListPreference) findPreference ("colour");
colour.setDefaultValue(mycolour);
Run Code Online (Sandbox Code Playgroud)
但它不起作用,因为它使第一选择成为默认选择.你能解释一下如何制作另一个默认的吗?谢谢.
我有一个文本到语音应用程序,用户可以选择一种语言,并选择男性或女性的声音.问题是,对于每种语言,有不同的字符串用于调用男性和女性的声音,但在我看来,我只有两种选择(男性和女性).
<string-array name="Language">
<item>English (US)</item>
<item>English (UK)</item>
<item>French (France)</item>
<item>Spanish (Spain)</item>
<item>Italian</item>
</string-array>
<string-array name="languageAlias">
<item>"en-US"</item>
<item>"en-GB"</item>
<item>"fr-FR"</item>
<item>"es-ES"</item>
<item>"it-IT"</item>
</string-array>
<string-array name="Voice">
<item>Male</item>
<item>Female</item>
</string-array>
<string-array name="VoiceAlias">
<item>"usenglishmale"</item>
<item>"usenglishfemale"</item>
<item>"ukenglishmale"</item>
<item>"ukenglishfemale"</item>
<item>"eurfrenchmale"</item>
<item>"eurfrenchfemale"</item>
<item>"eurspanishmale"</item>
<item>"eurspanishfemale"</item>
<item>"euritalianmale"</item>
<item>"euritalianfemale"</item>
</string-array>
Run Code Online (Sandbox Code Playgroud)
我试图找到一种方法,只根据选择的语言参考相关的男性和女性语音.是否可以在此处执行此操作,还是必须编写一些代码,根据所选语言更改voiceAlias数组的值?
提前致谢
这是我的第一个Android应用程序,我在尝试使用ListPreference时遇到异常.应用程序加载首选项...但是当我触摸ListPreference条目时,应用程序"意外停止".
Settings.java
public class Settings extends PreferenceActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.addPreferencesFromResource(R.layout.settings);
}
}
Run Code Online (Sandbox Code Playgroud)
的settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="chk_enabled"
android:summary="SMS response based on settings"
android:title="Enable"
/>
<ListPreference
android:title="Contacts"
android:summary="Contacs that will be sent SMSs"
android:key="list_contacts"
android:defaultValue="0"
android:entries="@array/list_entries"
android:entryValues="@array/list_values"
/>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="list_entries">
<item>All</item>
<item>WhiteList</item>
<item>BlackList</item>
</string-array>
<integer-array name="list_values">
<item>0</item>
<item>1</item>
<item>2</item>
</integer-array>
</resources>
Run Code Online (Sandbox Code Playgroud)
这是logcat输出:
D/AndroidRuntime( 3187): >>>>>>>>>>>>>> AndroidRuntime START …
Run Code Online (Sandbox Code Playgroud) 我试图创建一个ListPreference
但不知何故禁用其中一个项目.有点像灰色或其他东西,没有能力选择它.这将是一个即将推出的功能,我希望它在列表中是不可选择的.
我已经创建了一个自定义ListPreference
类,并在该类中自定义适配器,希望使用适配器来创建我想要的.
代码工作,它设置适配器,但没有调用任何适配器函数.我在方法上设置断点,例如getCount()
但它们永远不会被调用.
这是我的代码.自定义ListPreference取自http://blog.350nice.com/wp/archives/240
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.preference.ListPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.app.AlertDialog.Builder;
public class CustomListPreference extends ListPreference {
private boolean[] mClickedDialogEntryIndices;
CustomListPreferenceAdapter customListPreferenceAdapter = null;
Context mContext;
public CustomListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mClickedDialogEntryIndices = new boolean[getEntries().length];
}
@Override
protected void onPrepareDialogBuilder(Builder builder) {
CharSequence[] entries = getEntries();
CharSequence[] entryValues = getEntryValues();
if (entries == null || entryValues …
Run Code Online (Sandbox Code Playgroud) 我的XML文件 ListPreference
<ListPreference android:key="lpBirim" android:title="Birim"
android:summary="" android:defaultValue="0" android:persistent="false"/>
Run Code Online (Sandbox Code Playgroud)
如何获取所选文本和所选值?
如何创建一个ListPreference
与checkbox
?我知道如何使用ListPreference
,但我需要在"重复"首选项的警报应用程序中进行多项选择.
像这个截图:
我试图设置ListPreference的默认值,但没有显示任何内容.
你可以检查我的代码是否有任何错误?
谢谢.
真的,Emad
这是在settings.xml文件中:
<PreferenceCategory android:title="Media:">
<CheckBoxPreference android:key="ChimeWhenMusicIsPlaying"
android:title="@string/ChimeWhenMusicIsPlayingTitle" android:summary="@string/ChimeWhenMusicIsPlayingSummary"
android:defaultValue="false" />
<ListPreference android:title="Chime Volume"
android:key="ChimeVolume" android:summary="Select volume for the chiming sound."
android:entries="@array/chimeVolumeLabels" android:entryValues="@array/chimeVolumeValues"
android:defaultValue="1" />
</PreferenceCategory>
Run Code Online (Sandbox Code Playgroud)
这是在数组文件中:
<resources>
<string-array name="chimeVolumeLabels">
<item>Default</item>
<item>Soft</item>
<item>Medium</item>
<item>Loud</item>
</string-array>
<string-array name="chimeVolumeValues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
</resources>
Run Code Online (Sandbox Code Playgroud) 我正在尝试以编程方式创建一个ListPreference
,我可以做,但是当我选择它时,它的条目列表是空的.我相信我正确设置setEntries()
和setEntryValues()
使用CharSequence
阵列,但是当我选择它,它只是空的.
请在下面的ActivitySetting
课程中找到.请注意我使用的PreferenceFragments
是不使用弃用的方法.但我只有一个PreferenceFragment
当前设置为默认值
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragment()).commit();
}
public static class PrefsFragment extends PreferenceFragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
// Create the new ListPref
ListPreference customListPref = new ListPreference(getActivity());
// Get the Preference Category which we want to add the ListPreference …
Run Code Online (Sandbox Code Playgroud) 我试图改变ListPreference的弹出对话框的样式,就像我在这个答案中看到的那样.例如,我想要对话框使用不同的背景颜色.
到目前为止,我尝试将自定义样式应用于:
<item name="android:dialogTheme">@style/AlertDialogStyle</item>
<item name="android:alertDialogTheme">@style/AlertDialogStyle</item>
<item name="android:alertDialogStyle">@style/AlertDialogStyle</item>
<item name="android:dialogPreferenceStyle">@style/AlertDialogStyle</item>
<style name="AlertDialogStyle" parent="AlertDialog.AppCompat">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColor">@color/lightGrey</item>
<item name="android:background">@color/cardBackground</item>
<item name="android:popupBackground">@color/cardBackground</item>
<item name="android:windowBackground">@color/cardBackground</item>
<item name="android:itemBackground">@color/cardBackground</item>
</style>
Run Code Online (Sandbox Code Playgroud)
但我的风格仍未应用/背景颜色不变.
这就是我的ListPreference的弹出对话框当前的样子:
这是我想要存档的颜色主题(基本上我用于其他对话框的主题相同):
要快速重现我的问题 - >我的项目是在github上
android ×10
listpreference ×10
preference ×2
checkbox ×1
java ×1
listadapter ×1
preferences ×1
selected ×1
settings ×1