自定义列表首选项呈现问题

时间:2016-01-07 21:17:18

标签: android listpreference typed-arrays

当我尝试预览我的偏好时,我收到此错误

  

渲染问题java.lang.UnsupportedOperationException at   android.content.res.BridgeResources.obtainTypedArray(BridgeResources.java:472)   在   com.grozeaion.www.gvicameraremotecontrol.MyListPreference。(MyListPreference.java:52)   at java.lang.reflect.Constructor.newInstance(Constructor.java:526)   在   android.preference.GenericInflater.createItem(GenericInflater.java:385)   在   android.preference.GenericInflater.createItemFromTag(GenericInflater.java:432)   在   android.preference.GenericInflater.rInflate(GenericInflater.java:483)   在   android.preference.GenericInflater.rInflate(GenericInflater.java:495)   在   android.preference.GenericInflater.inflate(GenericInflater.java:327)   在   android.preference.Preference_Delegate.inflatePreference(Preference_Delegate.java:64)   将堆栈复制到剪贴板

我创建了一个自定义列表首选项,如下所示

  1. attr.xml我有

    <declare-styleable name="MyListPreference">
    <attr name="android:id" />
    <attr name="android:key" />
    <attr name="android:entries" />
    <attr name="android:entryValues" />
    <attr name="android:defaultValue" />
    <attr name="itemIco" format="integer" />
    

  2. 我在preferences.xml中定义了控件

        <com.grozeaion.www.gvicameraremotecontrol.MyListPreference
        android:id="@+id/Dev2UseAs"
        android:icon="@drawable/off"
        android:key="Dev2UseAsK"
        android:summary="@string/trig_dev2m_s"
        android:title="@string/trig_dev2m_t"
        android:entries="@array/modeDevName"
        android:entryValues="@array/modeDevVal"
        android:defaultValue="0"
        custom:itemIco="@array/modeDevIco"
        />
    
  3. 数组modeDevIco看起来像这样

    <array name="modeDevIco">
    <item>@drawable/off</item>
    <item>@drawable/camera</item>
    <item>@drawable/flash</item>
    <item>@drawable/split</item>
    

  4. 在我的自定义类

    public MyListPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    setLayoutResource(R.layout.my_list_value_main);
    this.context = context;
    Resources resources = context.getResources();
    preferences = PreferenceManager.getDefaultSharedPreferences(context);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyListPreference, 0, 0);
    try {
        crtCtrlKey = a.getString(R.styleable.MyListPreference_android_key);
        itemTitle = resources.getStringArray(a.getResourceId(R.styleable.MyListPreference_android_entries, 0));
        itemVal = resources.getStringArray(a.getResourceId(R.styleable.MyListPreference_android_entryValues, 0));
        defVal = a.getString(R.styleable.MyEditText_android_defaultValue);
        int len = itemVal.length;
        itemIco = new int[len];
        TypedArray iDs = resources.obtainTypedArray(a.getResourceId(R.styleable.MyListPreference_itemIco, 0));//get ID's for icons
        for (int i = 0; i < len; i++) {
            itemIco[i] = iDs.getResourceId(i,0);
        }
        iDs.recycle();
    } finally {
        a.recycle();
    }
    

    }

  5. 第52行

    TypedArray iDs = resources.obtainTypedArray(a.getResourceId(R.styleable.MyListPreference_itemIco, 0));//get ID's for icons
    

    如何修复此渲染问题?我猜这是因为我如何获得图标ID,但我不知道如何做到这一点

1 个答案:

答案 0 :(得分:3)

我有相同的渲染问题。但我正在努力获取色彩资源。我是通过isInEditMode()来解决的。

private int[] getColorsById(int id){
    if(isInEditMode()){
        String[] s=mContext.getResources().getStringArray(id);
        int[] colors = new int[s.length];
        for (int j = 0; j < s.length; j++){
            colors[j] = Color.parseColor(s[j]);
        }
        return colors;
    }else{
        TypedArray typedArray=mContext.getResources().obtainTypedArray(id);
        int[] colors = new int[typedArray.length()];
        for (int j = 0; j < typedArray.length(); j++){
            colors[j] = typedArray.getColor(j,Color.BLACK);
        }
        typedArray.recycle();
        return colors;
    }
}

我希望它可以帮到你很多。或者你有更好的方法来解决它。只是评论我。