2013-03-15 82 views
3

我写一个自定义的DialogPreference我设置有2个阵列命名entryValues,字符串的偏好相似什么ListPreference需要。尽管当我的构造函数被AttributeSet参数调用时,这两个数组似乎都存在,但试图通过getsStyledAttributes获取样式化数组失败,返回null。obtainStyledAttributes未能找到阵列

我在做什么错,我该如何解决这个问题?

我的自定义类的代码是:

public class BackgroundPicker extends DialogPreference {  

    private CharSequence[] mEntries; 
    private CharSequence[] mEntryValues; 

    public BackgroundPicker(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 

     // Print all values in attrs AttributeSet: 
     for (int i=0;i<attrs.getAttributeCount();i++) { 
      String name = attrs.getAttributeName(i); 
      Object value = attrs.getAttributeValue(i); 
      Log.d(TAG, name + " : " + value + " of type " + value.getClass().getSimpleName()); 
     } 

     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ListPreference, 0, 0); 

     // getIndexCount returns zero ! 
     for(int i=0; i<a.getIndexCount(); i++) { 
      TypedValue v = a.peekValue(i); 
      Log.d(TAG, "value[" + i + "] = " + v.toString()); 
     } 

     // Both entries and entryValues are null. 
     mEntries = a.getTextArray(R.styleable.ListPreference_entries); 
     mEntryValues = a.getTextArray(R.styleable.ListPreference_entryValues); 
    } 
} 

我调试打印是:

BackgroundPicker CTOR: IN 
BackgroundPicker CTOR param attrs: 
entries : @2131099650 of type String 
title : @2131165247 of type String 
key : BackgroundImage of type String 
summary : @2131165248 of type String 
defaultValue : back.png of type String 
entryValues : @2131099651 of type String 
BackgroundPicker CTOR param defStyle = 0 
BackgroundPicker CTOR: OUT 

我的风格在styles.xml

<declare-styleable name="ListPreference"> 
    <attr name="entries" format="reference" /> 
    <attr name="entryValues" format="reference" /> 
</declare-styleable>  

我的数据arrays.xml

<array name="Backgrounds"> 
    <item>back.png</item> 
    <item>one.png</item> 
</array> 

和我的喜好设置:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 
    <PreferenceCategory android:title="@string/generaloptions" > 
     <BackgroundPicker 
      android:defaultValue="back.png" 
      android:entries="@+array/Backgrounds" 
      android:entryValues="@+array/Backgrounds" 
      android:key="BackgroundImage" 
      android:summary="@string/backgroundImageSummary" 
      android:title="@string/backgroundImage" /> 
    </PreferenceCategory> 
</PreferenceScreen> 

回答

3

与+符号的数值看起来很奇怪,我从来没有使用过在我的设置,也许这​​是不正确的行为的主要原因。只需使用android:entries="@array/Backgrounds"而无需+

也可以尝试使用我的代码来获取值,至少它为整数类型的属性,所以它应该用于其他类型的工作,以及:

TypedArray a = context.obtainStyledAttributes(attrs, new int[] { R.attr.entries, R.attr.entryValues }); 
mEntries = a.getTextArray(0); // because R.attr.entries has index 0 in the passed array 
mEntryValues = a.getTextArray(1); 
+0

不幸的是这并没有解决这个问题。 getTextArray返回null。 a.getIndexCount()甚至返回null,尽管一个变量的长度为2并且似乎有数据。我尝试从条目的定义,其他获取函数等中删除android:现在,我正在提取引用的资源,unstyled并使用它(丑陋,我知道)。 – TudorT 2013-03-16 14:07:56

+0

@TudoT实际上android已经有属性'entries'和'entryValues',你不应该重新定义它们。也尝试添加android命名空间到我的例子,如'android.R.attr.entries' – vorrtex 2013-03-16 17:52:24

+0

非常感谢!使用android.R.attr.entries解决了这个问题。 PackageName.R.attr.entries不起作用。使用自定义名称而不是“条目”和“entryValues”也不起作用。所以这是一个命名空间问题。 – TudorT 2013-03-16 23:22:39