0

我收到以下错误消息

android.content.res.Resources$NotFoundException: If the resource you are trying to use is a vector resource, you may be referencing it in an unsupported way. See AppCompatDelegate.setCompatVectorFromResourcesEnabled() for more info. 

当我尝试设置以下:

view.setBackgroundResource(R.drawable.highlight_background); 

view.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.highlight_background)); 

我也使用AppCompatImage尝试。这发生在Android 4.4.4的设备上。我发现另一个StackOverflow的线程提供补充

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); 

成MyApplication的类和

vectorDrawables.useSupportLibrary = true 

到的build.gradle。但错误仍然存​​在。 drawable由以下几部分组成:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <stroke android:width="2dp" android:color="?attr/colorAccent" /> 
</shape> 

这只是一条包围图像的线。谁能帮我?

在此先感谢!

+0

你的问题是android_color = attr/colorAccent“”行,看下面的链接 –

回答

2

view.setBackgroundDrawable()已过时,使用 view.setBackgroundResource(int resId)代替

例如,

yourview.setBackgroundResource(R.drawable.highlight_background); 

和offcourse改变这样

android:color="@color/color_defined_in_colors_xml_file" 

你的颜色值希望这将有助于。

+0

仔细阅读这个问题。他说:“当我尝试设置以下内容: view.setBackgroundResource(R.drawable.highlight_background);” –

+0

对不起,我编辑了答案。 – AndroidGeek

+0

你和an_droid_dev是正确的。将android:color =“?attr/colorAccent”更改为android:color =“@ color/colorAccent”的技巧。谢谢! – Lars

0

你可以使用imageView.setImageResource();

相关问题