user2701206 user2701206 - 9 hours ago 2
Android Question

Resources$NotFoundException: If the resource you are trying to use is a vector resource, you may be referencing it in an unsupported way

im getting the following error message

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.


when im trying to set the following:

view.setBackgroundResource(R.drawable.highlight_background);


or

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


I also tried using AppCompatImage. This happens on a device with Android 4.4.4. Ive found another stackoverflow thread which provides to add

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);


into an MyApplication class and

vectorDrawables.useSupportLibrary = true


into the build.gradle. But the error still occurs. The drawable consists of the following:

<?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>


Its just a line to encircle an image. Can anyone help me?

Thanks in advance!

Answer

view.setBackgroundDrawable() is deprecated, use view.setBackgroundResource(int resId) instead

For example,

yourview.setBackgroundResource(R.drawable.highlight_background);

and offcourse change your color value like this

android:color="@color/color_defined_in_colors_xml_file"

Hope it will help.