2016-01-07 11 views
1

當我嘗試預覽我的喜好,我得到這個錯誤自定義列表偏好渲染問題

呈現問題java.lang.UnsupportedOperationException在 android.content.res.BridgeResources.obtainTypedArray(BridgeResources.java:472) 在 com.grozeaion.www.gvicameraremotecontrol.MyListPreference。(MyListPreference.java:52) 在java.lang.reflect.Constructor.newInstance(Constructor.java:526) 在 android.preference.GenericInflater.createItem(GenericInflater .java:385) at 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(); 
    } 
    

    }

線52

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

我怎麼能解決這個問題的渲染?我猜這是因爲我如何獲得圖標ID,但我不知道如何去做,否則

回答

2

我有相同的呈現問題。但我正在嘗試獲取色彩資源。我通過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; 
    } 
} 

我希望它能幫助你很多。或者你有更好的方法來弄清楚。只是評論我。

+0

我想這解決了手頭的問題......也幫了我 – Juni

+0

這並沒有回答這個問題。 'isInEditMode'是使用正確的東西,但它不能從擴展的'Preference'類中訪問。我有這個相同的問題,我正在擴展一個'EditTextPreference',但不能從該類調用'isInEditMode'。 – vane