android.content.Res.Resources $ notFoundException在编程设置android.r.attr.listchoiceindicatormultife时

StackOverflow https://stackoverflow.com/questions/4783002

我正在尝试在ListView中使用的CheckEdTextView项上编程设置“ Android:Checkmark”属性。运行我的应用程序时,我会得到以下例外:

android.content.res.Resources$NotFoundException: Resource ID #0x101021a

具有ID#0x101021a的资源对应于 android.r.attr.listchoiceindicatormultiph, ,这正是我传递给我的checkedtextview的价值:

mCheckedTextView.setCheckMarkDrawable(android.R.attr.listChoiceIndicatorMultiple)

这不是从Java做到的方式吗?我尝试(并成功)触发XML布局所需的行为:

<CheckedTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:id="@android:id/text1" />

问题是我不知道编译是否应该

android:checkMark="?android:attr/listChoiceIndicatorMultiple"

或者

android:checkMark="?android:attr/listChoiceIndicatorSingle"

因此,我需要在运行时设置这些值。

有帮助吗?

解决方案

我猜想,以编程方式设置 属性 参考而不是 Drawable 参考是问题。

在这种情况下, android.R.attr.listChoiceIndicatorMultiple 对应于 android.R.drawable.btn_check, ,因此您可以尝试设置该设置。


或者,如果您可以获得属性,则可以致电 getDrawable()TypedArray 动态获取 Drawable 价值。

编辑:
由于价值 listChoiceIndicatorMultiple 取决于当前主题,您需要要求当前主题来解决参考:

int[] attrs = { android.R.attr.listChoiceIndicatorMultiple };
TypedArray ta = getContext().getTheme().obtainStyledAttributes(attrs);
Drawable indicator = ta.getDrawable(0);
view.setCheckMarkDrawable(indicator);
ta.recycle();

确保缓存可抽签,而不是为您的每个项目执行此操作 ListView.

这只是一个非常基本的示例,但它可以与默认主题一起使用。我不确定要解决什么需要做什么 attrs 如果您有自定义主题,则完全。

其他提示

如果使用AppCompat库简单的替代方法是:

setCheckMarkDrawable(android.support.v7.appcompat.R.drawable.abc_btn_check_material);

或者:

setCheckMarkDrawable(android.support.v7.appcompat.R.drawable.abc_btn_radio_material);  
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top