当我尝试预览我的首选项时,出现此错误
Rendering Problems java.lang.UnsupportedOperationException at android.content.res.BridgeResources.obtainTypedArray(BridgeResources.java:472) at com.grozeaion.www.gvicameraremotecontrol.MyListPreference.(MyListPreference.java:52) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at android.preference.GenericInflater.createItem(GenericInflater.java:385) at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:432) at android.preference.GenericInflater.rInflate(GenericInflater.java:483) at android.preference.GenericInflater.rInflate(GenericInflater.java:495) at android.preference.GenericInflater.inflate(GenericInflater.java:327) at android.preference.Preference_Delegate.inflatePreference(Preference_Delegate.java:64) Copy stack to clipboard
我创建了一个自定义列表首选项,如下所示
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" />
我在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" />
数组模式DevIco看起来像这样
<array name="modeDevIco"> <item>@drawable/off</item> <item>@drawable/camera</item> <item>@drawable/flash</item> <item>@drawable/split</item>
在我的自定义类中
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 的方式不同,但我不知道该怎么做
最佳答案
我也有同样的渲染问题。但我正在尝试获取颜色资源。我通过 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;
}
}
希望对你有很大帮助。或者你有更好的方法来解决这个问题。就评论我吧。
关于android - 自定义列表首选项渲染问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34665332/