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

2

15

I'm 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. I've found another StackOverflow thread which provides to add

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

into a 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>

It's just a line to encircle an image. Can anyone help me?

Thanks in advance!

Haemoid answered 17/7, 2017 at 13:6 Comment(1)
your problem is android_color=attr/colorAccent"" line , see link belowInfundibuliform
S
8

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.

Splendiferous answered 17/7, 2017 at 13:19 Comment(3)
read the question carefully. He said "when im trying to set the following: view.setBackgroundResource(R.drawable.highlight_background);"Infundibuliform
yeah sorry i edited the answer.Splendiferous
You and an_droid_dev are right. Changing android:color="?attr/colorAccent" to android:color="@color/colorAccent" did the trick. Thank you!Haemoid
E
2

you can use imageView.setImageResource();

Eolith answered 17/7, 2017 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.