Binary XML file line #1: invalid drawable tag vector
Solution 1
implementation 'androidx.appcompat:appcompat:{current_version}'
If the issue is caused by a drawableLeft
or drawableRight
then replace TextView
with androidx.appcompat.widget.AppCompatTextView
or
EditText
with androidx.appcompat.widget.AppCompatEditText
.
Then, use one of:
app:drawableLeftCompat
app:drawableStartCompat
app:drawableEndCompat
app:drawableRightCompat
Solution 2
Documentation about Vector Graphics says:
Android 4.4 (API level 20) and lower doesn't support vector drawables.
With Support Library you have backward-compatibility using the attribute app:srcCompat
, but it is not backported for android:drawableRight
.
The solution is to keep using .PNG files for those cases or try to set it by code.
Solution 3
To create vector drawable use:
vector = VectorDrawableCompat.create(context.getResources(), R.drawable.res, null);
Or add AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
to Application onCreate()
method
Note: this solution doesn't work for android:drawableLetf or android:drawableRight but allow to use app:srcCompat
Solution 4
You can use this solution to get vector drawables before 5.0 :
Drawable drawable = AppCompatResources.getDrawable(mContext, mImageTitleResId);
Ace Falobi
Updated on June 16, 2022Comments
-
Ace Falobi almost 2 years
I have an app that runs perfectly on most devices. However, I'm getting a FATAL exception whenever I try to run my application on devices with API < 21.
Here is the log:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aceinteract.sleak/com.aceinteract.sleak.activity.LoginRegisterActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class EditText at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) at android.app.ActivityThread.access$700(ActivityThread.java:158) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5365) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method) Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class EditText at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710) at android.view.LayoutInflater.rInflate(LayoutInflater.java:752) at android.view.LayoutInflater.parseInclude(LayoutInflater.java:846) at android.view.LayoutInflater.rInflate(LayoutInflater.java:742) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.inflate(LayoutInflater.java:495) at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60) at android.view.LayoutInflater.inflate(LayoutInflater.java:397) at android.view.LayoutInflater.inflate(LayoutInflater.java:353) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) at com.aceinteract.sleak.activity.LoginRegisterActivity.onCreate(LoginRegisterActivity.kt:21) at android.app.Activity.performCreate(Activity.java:5326) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) at android.app.ActivityThread.access$700(ActivityThread.java:158) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5365) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method) Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_person_accent_24dp.xml from drawable resource ID #0x7f07006f at android.content.res.Resources.loadDrawable(Resources.java:2842) at android.content.res.TypedArray.getDrawable(TypedArray.java:602) at android.widget.TextView.<init>(TextView.java:1023) at android.widget.EditText.<init>(EditText.java:76) at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:64) at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:60) at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:112) at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1016) at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1073) at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater$WrapperFactory2.onCreateView(CalligraphyLayoutInflater.java:280) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:681) at android.view.LayoutInflater.rInflate(LayoutInflater.java:752) at android.view.LayoutInflater.parseInclude(LayoutInflater.java:846) at android.view.LayoutInflater.rInflate(LayoutInflater.java:742) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.inflate(LayoutInflater.java:495) at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60) at android.view.LayoutInflater.inflate(LayoutInflater.java:397) at android.view.LayoutInflater.inflate(LayoutInflater.java:353) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) at com.aceinteract.sleak.activity.LoginRegisterActivity.onCreate(LoginRegisterActivity.kt:21) at android.app.Activity.performCreate(Activity.java:5326) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) at android.app.ActivityThread.access$700(ActivityThread.java:158) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5365) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method) Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:917) at android.graphics.drawable.Drawable.createFromXml(Drawable.java:858) at android.content.res.Resources.loadDrawable(Resources.java:2839) at android.content.res.TypedArray.getDrawable(TypedArray.java:602) at android.widget.TextView.<init>(TextView.java:1023) at android.widget.EditText.<init>(EditText.java:76) at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:64) at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:60) at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:112) at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1016) at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1073) at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater$WrapperFactory2.onCreateView(CalligraphyLayoutInflater.java:280) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:681) at android.view.LayoutInflater.rInflate(LayoutInflater.java:752) at android.view.LayoutInflater.parseInclude(LayoutInflater.java:846) at android.view.LayoutInflater.rInflate(LayoutInflater.java:742) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.inflate(LayoutInflater.java:495) at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60) at android.view.LayoutInflater.inflate(LayoutInflater.java:397) at android.view.LayoutInflater.inflate(LayoutInflater.java:353) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) at com.aceinteract.sleak.activity.LoginRegisterActivity.onCreate(LoginRegisterActivity.kt:21) at android.app.Activity.performCreate(Activity.java:5326) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) at android.app.ActivityThread.access$700(ActivityThread.java:158) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5365) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method)
Here is the layout XML
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/layout_register" android:visibility="gone" xmlns:app="http://schemas.android.com/apk/res-auto"> <EditText android:id="@+id/edit_register_full_name" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" android:drawableEnd="@drawable/ic_person_accent_24dp" android:padding="20dp" android:layout_width="0dp" android:hint="@string/hint_full_name" android:singleLine="true" android:layout_height="wrap_content" android:inputType="textPersonName" android:drawableRight="@drawable/ic_person_accent_24dp" /> <EditText android:id="@+id/edit_register_email" app:layout_constraintTop_toBottomOf="@id/edit_register_full_name" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" android:drawableEnd="@drawable/ic_email_accent_24dp" android:padding="20dp" android:layout_width="0dp" android:hint="@string/hint_email" android:inputType="textEmailAddress" android:layout_height="wrap_content" android:drawableRight="@drawable/ic_email_accent_24dp" /> <EditText android:id="@+id/edit_register_password" app:layout_constraintTop_toBottomOf="@id/edit_register_email" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" android:drawableEnd="@drawable/ic_vpn_key_accent_24dp" android:padding="20dp" android:inputType="textPassword" android:layout_width="0dp" android:hint="@string/hint_password" android:layout_height="wrap_content" android:drawableRight="@drawable/ic_vpn_key_accent_24dp" /> <CheckBox android:id="@+id/check_register_show_password" android:text="@string/desc_show_password" android:layout_marginTop="10dp" app:layout_constraintTop_toBottomOf="@id/edit_register_password" app:layout_constraintLeft_toLeftOf="parent" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </android.support.constraint.ConstraintLayout>
And the gradle file:
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 27 defaultConfig { applicationId "com.aceinteract.sleak" minSdkVersion 16 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } buildToolsVersion '27.0.2' } androidExtensions { experimental = true } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation 'com.android.support:appcompat-v7:27.0.2' implementation 'com.android.support:design:27.0.2' implementation 'com.android.support:customtabs:27.0.2' implementation 'com.android.support:support-vector-drawable:27.0.2' implementation 'com.android.support:support-v4:27.0.2' implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.android.support:animated-vector-drawable:27.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' implementation 'com.android.support:cardview-v7:27.0.2' implementation 'com.android.support:gridlayout-v7:27.0.2' implementation 'com.android.support:recyclerview-v7:27.0.2' implementation 'com.android.support:design:27.0.2' implementation 'com.android.support:palette-v7:27.0.2' implementation 'com.google.code.gson:gson:2.8.0' implementation 'com.mikhaellopez:circularimageview:3.0.2' implementation 'com.squareup.retrofit2:retrofit:2.3.0' implementation 'com.squareup.retrofit2:converter-gson:2.3.0' implementation 'uk.co.chrisjenx:calligraphy:2.2.0' }
I have tried changing to AppComaptEditText, but it still just gives the same error.
Thanks in advance :)
-
Ace Falobi over 6 yearsThanks a lot. This pretty much fixes my problem
-
Capricorn almost 6 yearsThe enquirer is using vector drawables, removing that line from the gradle file would remove the support for vector drawables in pre API 20 devices.
-
abner almost 6 yearsCApricorn....... It can use the Support Library option, which allows it to use vector drawables in versions prior to api 20, as developer.android.com/studio/write/vector-asset-studio
-
Shashank Saxena over 5 yearsSetting it by code works? Or did you have to change the drawable to a PNG?
-
Murciegalo84 over 5 yearsNot necessary switch to PNG, you can play with your layout and and include your vector drawable as an image view.
-
Arsenius about 5 yearsYour solution doesn't work for android:drawableLetf or android:drawableRight
-
Ace Falobi about 5 yearsThis is actually a fix to this problem. +1. Marking as answer
-
Peter almost 5 yearsI get the same error as OP when getting svg drawable from code on API < 21
-
xarlymg89 almost 5 yearsThe same fix can be applied when using Button and it's AppCompatButton counterpart.
-
Muhammad Babar over 2 yearsThanks. Working fine.
-
Emre A over 2 yearsThis should be a replacement for
ContextCompat.getDrawable
-
Dong Wang over 2 yearsI still got same issue in Android 4.4.2 by using this solution. I just wrap vector xml into layerlist drawable file. I don't know whether this make it error.
-
Vishal Bhandare over 2 years@abner was correct. To remove vectorDrawables.useSupportLibrary = true it works for me. i am not sure why he got downvote. if you read this article medium.com/androiddevelopers/… it mention This flag prevents the Android Gradle Plugin from generating PNG versions of your vector assets if your minSdkVersion is < 21—we don’t need this as if we use the AndroidX library instead.