I have the following situation: I have a constraint layout with background:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/cl_wave_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ic_wave_header_svg_drawable"
android:paddingBottom="41dp"
app:layout_constraintTop_toTopOf="parent">
Here's the ic_wave_header_svg_drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_wave_header" />
</layer-list>
Here's the ic_wave_header:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
xmlns:tools="http://schemas.android.com/tools"
android:width="328dp"
android:height="199dp"
android:viewportWidth="328"
android:viewportHeight="199"
tools:ignore="VectorRaster">
<path
android:fillType="evenOdd"
android:pathData="M328,198.147C328,198.147 287.806,164.737 164,168.868C40.194,172.999 0,140 0,140V198.147V0H328L328,198.147Z">
<aapt:attr name="android:fillColor">
<gradient
android:endX="351.346"
android:endY="198.147"
android:startX="351.346"
android:startY="-28.2069"
android:type="linear">
<item
android:color="#FF9DDE10"
android:offset="0" />
<item
android:color="#FF82BD00"
android:offset="1" />
</gradient>
</aapt:attr>
</path>
</vector>
When I run the app, it crashes on android 6.0 and lower. In the other versions, it works correctly. What's the problem and how can I solve it?
UPD
Error log:
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_wave_header.xml from drawable resource ID #0x7f080180
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2640)
at android.content.res.Resources.loadDrawable(Resources.java:2540)
at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
at android.graphics.drawable.LayerDrawable.updateLayerFromTypedArray(LayerDrawable.java:296)
at android.graphics.drawable.LayerDrawable.inflateLayers(LayerDrawable.java:239)
at android.graphics.drawable.LayerDrawable.inflate(LayerDrawable.java:164)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1215)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1124)
at android.content.res
It seems that it is something incorrect with drawable, but if it is right, why it doesn't crash on other devices
In your application class -> enable setCompatVectorFromResourcesEnabled
public class App extends Application {
#Override
public void onCreate() {
super.onCreate();
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);//add this
}
This crash is caused by using
android:offset="0" in svg file of your drawable.
To fix this you can either avoid using android:offset="0" in svg file (by using plain color instead of gradient) or you can use .png format of your drawable image for quick fix.
We have a drawable file already in drawable v21 also for ripple effect.
So why we use drawable v24 when we are getting vector file already.
Below is the xml File ic_launcher_foreground.xml.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportHeight="108"
android:viewportWidth="108">
<path
android:fillType="evenOdd"
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
android:strokeColor="#00000000"
android:strokeWidth="1">
<aapt:attr name="android:fillColor">
<gradient
android:endX="78.5885"
android:endY="90.9159"
android:startX="48.7653"
android:startY="61.0927"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0"/>
<item
android:color="#00000000"
android:offset="1.0"/>
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11
40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81
70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76
60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
android:strokeColor="#00000000"
android:strokeWidth="1"/>
</vector>
The different drawable folders are for providing different screen densities for device compatibility and for different Android versions.
See this answer for more details
I found there were numerous vector drawable crashes happened on pre-Lollipop devices and I can’t reproduce it.
One is caused by org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector when I use setImageResource
android.content.res.Resources$NotFoundException: File res/drawable/ic_mylib_offline_tracks_32.xml from drawable resource ID #0x7f020414
at android.content.res.Resources.loadDrawable(Resources.java:2141)
at android.content.res.Resources.getDrawable(Resources.java:711)
at android.support.v7.widget.VectorEnabledTintResources.superGetDrawable(VectorEnabledTintResources.java:74)
at android.support.v7.widget.AppCompatDrawableManager.onDrawableLoadedFromResources(AppCompatDrawableManager.java:433)
at android.support.v7.widget.VectorEnabledTintResources.getDrawable(VectorEnabledTintResources.java:67)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:360)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:85)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:93)
...
Code:
#override
protected void onBindItemViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
...
viewHoler.imageView.setImageResource(vectorResId);
...
}
Vector drawable - ic_mylib_offline_tracks_32.xml:
<vector android:height="24dp" android:viewportHeight="32.0"
android:viewportWidth="32.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#00000000" android:fillType="evenOdd"
android:pathData="M7,21v3.75c0,0.69 0.54,1.25 1.2,1.25h15.6c0.66,0 1.2,-0.56 1.2,-1.25V21M19,17l-3,2 -3,-2M16,6v12"
android:strokeColor="#A0A098" android:strokeLineCap="round"
android:strokeLineJoin="round" android:strokeWidth="1"/>
</vector>
And the others caused by android.content.res.Resources$NotFoundException: File res/drawable/ic_more_white_24.xml from drawable resource ID #0x7f020357. 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 I refresh toolbar menu visibility.
android.content.res.Resources$NotFoundException: File res/drawable/ic_more_white_24.xml from drawable resource ID #0x7f020357. 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.
at android.content.res.Resources.loadDrawable(Resources.java:2096)
at android.content.res.Resources.getDrawable(Resources.java:700)
at android.support.v7.widget.VectorEnabledTintResources.superGetDrawable(VectorEnabledTintResources.java:74)
at android.support.v7.widget.AppCompatDrawableManager.onDrawableLoadedFromResources(AppCompatDrawableManager.java:435)
at android.support.v7.widget.VectorEnabledTintResources.getDrawable(VectorEnabledTintResources.java:67)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:353)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:200)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
at android.support.v7.view.menu.MenuItemImpl.getIcon(MenuItemImpl.java:491)
at android.support.v7.view.menu.ActionMenuItemView.initialize(ActionMenuItemView.java:125)
at android.support.v7.widget.ActionMenuPresenter.bindItemView(ActionMenuPresenter.java:210)
at android.support.v7.view.menu.BaseMenuPresenter.getItemView(BaseMenuPresenter.java:187)
at android.support.v7.widget.ActionMenuPresenter.getItemView(ActionMenuPresenter.java:196)
at android.support.v7.widget.ActionMenuPresenter.flagActionItems(ActionMenuPresenter.java:476)
at android.support.v7.view.menu.MenuBuilder.flagActionItems(MenuBuilder.java:1164)
at android.support.v7.view.menu.BaseMenuPresenter.updateMenuView(BaseMenuPresenter.java:95)
at android.support.v7.widget.ActionMenuPresenter.updateMenuView(ActionMenuPresenter.java:229)
at android.support.v7.view.menu.MenuBuilder.dispatchPresenterUpdate(MenuBuilder.java:291)
at android.support.v7.view.menu.MenuBuilder.onItemsChanged(MenuBuilder.java:1051)
at android.support.v7.view.menu.MenuBuilder.onItemVisibleChanged(MenuBuilder.java:1090)
at android.support.v7.view.menu.MenuItemImpl.setVisible(MenuItemImpl.java:651)
...
Code:
public void refreshMenuItemVisibility(boolean visibility) {
toolbar.getMenu().findItem(R.id.menu_overflow).setVisible(visible);
}
Vector drawable - ic_more_white_24.xml:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="32dp"
android:height="32dp"
android:viewportWidth="32.0"
android:viewportHeight="32.0">
<path
android:pathData="M10.5,16.5m-1.5,0a1.5,1.5 0,1 1,3 0a1.5,1.5 0,1 1,-3 0"
android:fillType="evenOdd"
android:fillColor="#FFFFFF"/>
<path
android:pathData="M16.5,16.5m-1.5,0a1.5,1.5 0,1 1,3 0a1.5,1.5 0,1 1,-3 0"
android:fillType="evenOdd"
android:fillColor="#FFFFFF"/>
<path
android:pathData="M22.5,16.5m-1.5,0a1.5,1.5 0,1 1,3 0a1.5,1.5 0,1 1,-3 0"
android:fillType="evenOdd"
android:fillColor="#FFFFFF"/>
</vector>
I have already followed the step from the official document, but it doesn't work.
vectorDrawables.useSupportLibrary = true
public class MainActivity extends AppCompatActivity {
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
}
My library and tools version are as follow:
Android Support Library: 26.0.0
Build Tools: 26.0.0
Gradle: 3.3
Android Plugin: 2.3.3
UPDATE
All of the vectors are put in drawable folder.
Try this and see if working or not.
For me its working as i am using support library 23.4.0:
Add this to build.gradle
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
Add the following to onCreate of your Application class
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
For all xml views in which you are setting a vector drawable replace
android:src
with
app:srcCompat
and in the code replace this:
imageView.setImageResource(...);
with
imageView.setImageDrawable(...);
There are couple of things you need to fix.
1) First put your vector image in drawable folder instead of drawable-v24.
2) Also add support for vector drawables in gradle file.
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
3) There is also a work around for this. Make a resource file in your drawable folder and do something like this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/Vector drawable"/>
</layer-list>
I'm trying to use a VectorDrawable selector in one of my Fragments. However, the app gets crashed showing this error -
Exception while inflating vector
android.content.res.Resources$NotFoundException: File
res/drawable/$ic_lose_weight_icon__0.xml from color state list
resource ID #0x7f080023
The drawable file ic_lose_weight_icon.xml includes following -
<vector android:height="52dp" android:viewportHeight="52.0"
android:viewportWidth="53.0" android:width="52dp"
xmlns:aapt="http://schemas.android.com/aapt" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillType="nonZero"
android:pathData="M45.23,5.23a6.7,6.7 0,0 0,-4.93 -2.18h-10.22A10.02,10.02 0,0 0,24 1a10.02,10.02 0,0 0,-6.07 2.05L7.71,3.05c-1.87,0 -3.67,0.79 -4.93,2.18A6.72,6.72 0,0 0,1.02 10.3l0.44,5.99c0.03,0.38 0.35,0.66 0.72,0.63a0.68,0.68 0,0 0,0.62 -0.73L2.36,10.2A5.36,5.36 0,0 1,3.76 6.16a5.29,5.29 0,0 1,3.95 -1.75h8.74a10.41,10.41 0,0 0,-2.63 6.94c0,5.71 4.57,10.35 10.18,10.35 5.61,0 10.18,-4.64 10.18,-10.35 0,-2.67 -1,-5.1 -2.63,-6.94h8.74a5.29,5.29 0,0 1,3.95 1.75,5.36 5.36,0 0,1 1.4,4.04l-2.15,29.44c-0.2,2.8 -2.55,5 -5.35,5h-9.67a0.68,0.68 0,0 0,-0.67 0.68c0,0.38 0.3,0.68 0.67,0.68h9.67a6.66,6.66 0,0 0,4.56 -1.8,6.76 6.76,0 0,0 2.13,-4.46l2.15,-29.44a6.72,6.72 0,0 0,-1.75 -5.07zM32.84,11.35c0,4.95 -3.96,8.99 -8.84,8.99 -4.87,0 -8.84,-4.03 -8.84,-8.99 0,-4.96 3.96,-8.99 8.84,-8.99 4.87,0 8.84,4.03 8.84,8.99zM25.09,44.65L10.36,44.65c-2.84,0 -5.22,-2.17 -5.43,-4.94l-1.4,-19.13a0.68,0.68 0,0 0,-0.73 -0.63,0.68 0.68,0 0,0 -0.63,0.72l1.4,19.13a6.62,6.62 0,0 0,2.16 4.41A6.85,6.85 0,0 0,10.36 46h14.72a0.68,0.68 0,0 0,0.68 -0.68,0.68 0.68,0 0,0 -0.68,-0.68z" android:strokeWidth=".5">
<aapt:attr name="android:fillColor">
<gradient android:endX="46.98890716996074"
android:endY="24.500811517238617"
android:startX="1.9492992758750916"
android:startY="24.500811517238617" android:type="linear">
<item android:color="#FF44CFB9" android:offset="0.0"/>
<item android:color="#FF1EC2C9" android:offset="1.0"/>
</gradient>
</aapt:attr>
<aapt:attr name="android:strokeColor">
<gradient android:endX="46.98890716996074"
android:endY="24.500811517238617"
android:startX="1.9492992758750916"
android:startY="24.500811517238617" android:type="linear">
<item android:color="#FF44CFB9" android:offset="0.0"/>
<item android:color="#FF1EC2C9" android:offset="1.0"/>
</gradient>
</aapt:attr>
</path>
<path android:fillType="nonZero" android:pathData="M25.03,10.46a1.82,1.82 0,0 0,-1.27 -0.32L21.47,6.18c-0.2,-0.35 -0.71,-0.71 -1.02,-0.52 -0.32,0.18 -0.32,0.83 -0.12,1.18l2.3,3.98a1.72,1.72 0,0 0,-0.08 0.13c-0.49,0.87 -0.19,2.06 0.69,2.66 0.42,0.29 0.91,0.4 1.38,0.31 0.18,-0.04 0.35,-0.1 0.5,-0.19 0.25,-0.14 0.45,-0.35 0.6,-0.61 0.24,-0.42 0.3,-0.93 0.17,-1.43a2.13,2.13 0,0 0,-0.86 -1.23zM24.89,12.54c-0.11,0.2 -0.3,0.33 -0.52,0.38a0.86,0.86 0,0 1,-0.66 -0.15,1.01 1.01,0 0,1 -0.41,-0.58 0.91,0.91 0,0 1,0.08 -0.68,0.77 0.77,0 0,1 0.52,-0.38 0.86,0.86 0,0 1,0.66 0.15c0.41,0.28 0.56,0.85 0.33,1.26z">
<aapt:attr name="android:fillColor">
<gradient android:endX="26.884173301124573"
android:endY="10.739999532699585"
android:startX="21.1299991607666"
android:startY="10.739999532699585" android:type="linear">
<item android:color="#FF44CFB9" android:offset="0.0"/>
<item android:color="#FF1EC2C9" android:offset="1.0"/>
</gradient>
</aapt:attr>
</path>
<path android:fillColor="#FFF" android:fillType="evenOdd"
android:pathData="M43,42m-9,0a9,9 0,1 1,18 0a9,9 0,1 1,-18 0" android:strokeWidth="1.5">
<aapt:attr name="android:strokeColor">
<gradient android:endX="85.58492" android:endY="75.0"
android:startX="68.0" android:startY="75.0" android:type="linear">
<item android:color="#FF44CFB9" android:offset="0.0"/>
<item android:color="#FF1EC2C9" android:offset="1.0"/>
</gradient>
</aapt:attr>
</path>
<path android:fillType="evenOdd" android:pathData="M43.15,45.31L40.36,39.72A0.5,0.5 0,0 1,40.81 39h5.58a0.5,0.5 0,0 1,0.45 0.72l-2.79,5.58a0.5,0.5 0,0 1,-0.89 0z">
<aapt:attr name="android:fillColor">
<gradient android:endX="80.75140421012878"
android:endY="75.29140281677246"
android:startX="74.29622650146484"
android:startY="75.29140281677246" android:type="linear">
<item android:color="#FF44CFB9" android:offset="0.0"/>
<item android:color="#FF1EC2C9" android:offset="1.0"/>
</gradient>
</aapt:attr>
</path>
</vector>
In my build.gradle file, I've also added this -
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
Along with,
dependencies {
implementation "com.android.support:appcompatv7:27.1.0"
}
In the fragment in which I'm using the above drawable, I have this code -
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="8dp"
android:gravity="center_horizontal"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="#dimen/textSize3"
app:fontName="#string/fnt_roboto_light"
app:srcCompat="#drawable/ic_lose_weight_icon" />
And in my activity I have this code as well -
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
But whenever I launch my app in Android 5.0.1 Lollipop, the app immediately crashes with the above error.
Any help here would be really appreciated. Thank you.
I had a similar issue. Seems like removing android:fillType="..." solved it for me. Not sure if the attribute it's needed in your case.
I faced the same issue, Updating my gradle version from 3.0.1 to 3.1.0 solved my problem.
Use belove line in your project level gradle file inside dependencies block:
classpath 'com.android.tools.build:gradle:3.1.0'