I've looked all over stackoverflow and no question was able to solve my problem.
I'm trying to play an animated vector drawable on a device with API 15 but I keep getting the following runtime exception:
com.karim.test E/AvdcInflateDelegate: Exception while inflating <animated-vector>
android.content.res.Resources$NotFoundException: File res/drawable-ldpi-v4/feedback.png from xml type xml resource ID #0x7f020066
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2152)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2107)
at android.content.res.Resources.getXml(Resources.java:906)
at android.support.graphics.drawable.VectorDrawableCompat.create(VectorDrawableCompat.java:408)
at android.support.graphics.drawable.AnimatedVectorDrawableCompat.inflate(AnimatedVectorDrawableCompat.java:356)
at android.support.graphics.drawable.AnimatedVectorDrawableCompat.createFromXmlInner(AnimatedVectorDrawableCompat.java:162)
at android.support.v7.widget.AppCompatDrawableManager$AvdcInflateDelegate.createFromXmlInner(AppCompatDrawableManager.java:905)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:356)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:197)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:192)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:66)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:71)
at com.karim.test.MainActivity.initialize(MainActivity.java:428)
at com.karim.test.MainActivity.onCreate(MainActivity.java:210)
Caused by: java.io.FileNotFoundException: Corrupt XML binary file
at android.content.res.AssetManager.openXmlAssetNative(Native Method)
at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:487)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2134)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2107)
at android.content.res.Resources.getXml(Resources.java:906)
at android.support.graphics.drawable.VectorDrawableCompat.create(VectorDrawableCompat.java:408)
at android.support.graphics.drawable.AnimatedVectorDrawableCompat.inflate(AnimatedVectorDrawableCompat.java:356)
at android.support.graphics.drawable.AnimatedVectorDrawableCompat.createFromXmlInner(AnimatedVectorDrawableCompat.java:162)
at android.support.v7.widget.AppCompatDrawableManager$AvdcInflateDelegate.createFromXmlInner(AppCompatDrawableManager.java:905)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:356)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:197)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:192)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:66)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:71)
at com.karim.test.MainActivity.initialize(MainActivity.java:428)
at com.karim.test.MainActivity.onCreate(MainActivity.java:210)
android.content.res.Resources$NotFoundException: File res/drawable/animated_feedback_left.xml from drawable resource ID #0x7f020065
at android.content.res.Resources.loadDrawable(Resources.java:1923)
at android.content.res.Resources.getDrawable(Resources.java:664)
at android.support.v7.widget.ResourcesWrapper.getDrawable(ResourcesWrapper.java:128)
at android.support.v7.widget.TintResources.getDrawable(TintResources.java:45)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:323)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:201)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:66)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:71)
at com.karim.test.MainActivity.initialize(MainActivity.java:422)
at com.karim.test.MainActivity.onCreate(MainActivity.java:204)
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag animated-vector
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:863)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:804)
at android.content.res.Resources.loadDrawable(Resources.java:1920)
at android.content.res.Resources.getDrawable(Resources.java:664)
at android.support.v7.widget.ResourcesWrapper.getDrawable(ResourcesWrapper.java:128)
at android.support.v7.widget.TintResources.getDrawable(TintResources.java:45)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:323)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:201)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:66)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:71)
at com.karim.test.MainActivity.initialize(MainActivity.java:422)
at com.karim.test.MainActivity.onCreate(MainActivity.java:204)
I'm using the same resources from my previous question that you can find over here Animated Vector Drawable not working with API 15
Also I've tested instead of using ImageView to use AppCompatImageView but still same error.
I've been debugging this error and it fails on the following statement:
final XmlPullParser parser = res.getXml(resId);
inside:
public static VectorDrawableCompat create(#NonNull Resources res, #DrawableRes int resId, #Nullable Theme theme)
method of VectorDrawableCompat verion 23.3.0.
It seems that I'm having an issue with compatibility. At some point Android decides to use the XML parsing of API 15 instead of using the compat version. Any ideas on how I can solve this annoying problem?
Update:
I followed Chris Banes blog and still no luck. Basically I've updated the compat libraries to 23.4.0 and I used the following in my activity:
static {
AppCompatDelegate.setCompatVectorFromSourcesEnabled(true);
}
Inflating Drawable's
`VectorDrawable` and `AnimatedVectorDrawable` in this support library can be inflated in this way:
Calling static getDrawable() methods:
//This will only inflate a drawable with <vector> as the root element
VectorDrawable.getDrawable(context, R.drawable.ic_arrow_vector);
//This will only inflate a drawable with <animated-vector> as the root element
AnimatedVectorDrawable.getDrawable(context, R.drawable.ic_arrow_to_menu_animated_vector);
// This will inflate any drawable and will auto-fallback to the lollipop implementation on api 21+ devices
ResourcesCompat.getDrawable(context, R.drawable.any_drawable);
If inflating the Drawable in java code, it is recommended to always use ResourcesCompat.getDrawable() as this handles Lollipop fallback when applicable. This allows the system to cache Drawable ConstantState and hence is more efficient.
The library has the following morph (bi-directional) animations :
Play-Pause morph animation
Play-Stop morph animation
Arrow-Hamburger menu morph animation
As you can see, I produced the above image on my API 16 phone:
import com.wnafee.vector.compat.AnimatedVectorDrawable;
mdrawable = (AnimatedVectorDrawable) AnimatedVectorDrawable.getDrawable(this.getApplicationContext(), R.drawable.consolidated_animated_vector);
Look at the github README for vector-compat here: https://github.com/wnafee/vector-compat
This will fix your problem (down to API 14) if you merge it with your app module's build.gradle dependencies (usually at the end of file):
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//Trying to FIX Binary XML file line #2: invalid drawable tag animated-vector
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
//not needed
// compile 'com.android.support:support-vector-drawable:25.0.0'
compile 'com.wnafee:vector-compat:1.0.5'//*******holy grail *******https://github.com/wnafee/vector-compat
// Failed to resolve: com.android.support:support-animated-vector-drawable:25.0.0
//not needed
// compile 'com.android.support:support-animated-vector-drawable:25.0.0'
}
There is support library 25.4.0 and it's intended to solve all our problems. For example it finally supports path morphing!
To use it add:
allprojects {
repositories {
maven {
url "https://maven.google.com"
}
}
}
inside your project gradle file, and accordingly dependency itself:
compile 'com.android.support:support-v4:25.4.0'
After that you can use vector drawable animation with path morphing (which is often the culprit behind "invalid drawable tag animated-vector") in below manner (a little Kotlin code):
val drawable = AnimatedVectorDrawableCompat.create(this, R.drawable.vector_drawable)
imageView.setImageDrawable(drawable) // You may use usual ImageView class, no Compat necessary.
drawable.start()
Related
I'm having some problems with Android Studio at the moment because I recently started using "Vector Assets". I've done absolutely everything necessary to display them correctly in my application (using the app:srcCompat="" in the xml, android {defaultConfig {vectorDrawables.useSupportLibrary true}} in the build.gradle, and AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); in the onCreate() method before setContentView()).
The svg files I imported into Android Studio do not have errors themselves, I can see the result without any problem in an ImageView with the wizard. The only problem comes when I run my application and it arrives at the time to draw the svg. Some of my svg's still work, while others don't, so I think some of them take too long to process.
Here is the error :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.retroverse.bataille_corse, PID: 18807
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.retroverse.bataille_corse/com.retroverse.bataille_corse.MenuPrincipal}: android.view.InflateException: Binary XML file line #127: Binary XML file line #127: Error inflating class ImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2985)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3120)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1840)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6872)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: android.view.InflateException: Binary XML file line #127: Binary XML file line #127: Error inflating class ImageView
Caused by: android.view.InflateException: Binary XML file line #127: Error inflating class ImageView
Caused by: android.content.res.Resources$NotFoundException: Drawable com.retroverse.bataille_corse:drawable/card_49_en with resource ID #0x7f060099
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/card_49_en.xml from drawable resource ID #0x7f060099
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:854)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:634)
at android.content.res.MiuiResourcesImpl.loadDrawable(MiuiResourcesImpl.java:329)
at android.content.res.Resources.getDrawableForDensity(Resources.java:902)
at android.content.res.Resources.getDrawable(Resources.java:841)
at android.content.Context.getDrawable(Context.java:644)
at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:454)
at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:144)
at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:132)
at androidx.appcompat.content.res.AppCompatResources.getDrawable(AppCompatResources.java:104)
at androidx.appcompat.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:59)
at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:78)
at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
at androidx.appcompat.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:187)
at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:107)
at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1407)
at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1457)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:867)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.retroverse.bataille_corse.MenuPrincipal.onCreate(MenuPrincipal.java:24)
at android.app.Activity.performCreate(Activity.java:7232)
E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:7221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2965)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3120)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1840)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6872)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.IllegalArgumentException: R is not a valid verb. Failure occurred at position 2 of path: STRING_TOO_LARGE
at android.util.PathParser.nCreatePathDataFromString(Native Method)
at android.util.PathParser.access$200(PathParser.java:24)
at android.util.PathParser$PathData.<init>(PathParser.java:76)
at android.graphics.drawable.VectorDrawable$VFullPath.updateStateFromTypedArray(VectorDrawable.java:2016)
at android.graphics.drawable.VectorDrawable$VFullPath.inflate(VectorDrawable.java:1967)
at android.graphics.drawable.VectorDrawable.inflateChildElements(VectorDrawable.java:819)
at android.graphics.drawable.VectorDrawable.inflate(VectorDrawable.java:717)
at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:142)
at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1332)
at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1291)
at android.content.res.ResourcesImpl.createFromXmlForDensity(ResourcesImpl.java:1506)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:838)
... 41 more
The most important line, I think, is Caused by: java.lang.IllegalArgumentException: R is not a valid verb. Failure occurred at position 2 of path: STRING_TOO_LARGE.
We also know that this svg was caught in the middle (its size is 187 kilobytes). So I think my SVG file (even imported in Android Studio by "Vector Asset") is too heavy.
As we are told here, the STRING_TOO_LARGE issue appears when the string "is longer than 0x7FFF = 32767 characters". (In the reproduction part)
Indeed, the only svg to work in my application are lighter than 32 kb, which could explain this.
But I really need to display this svg (don't offer to convert it to png!) correctly, and if possible without optimizing its paths.
Any help would be welcome!
I found the answer (at last...).
Actually, there is no real problem with Vector Assets over 32 kb, it all depends on what is in the code. I should have paid more attention to the warnings than usual I don't always look at...
I discovered inside my svg a path data with a path of more than 40kb (in one string so). The fact is that, according to the site in the question, a String can't have more than (about) 32k characters or the whole string used in any xml file will be replaced by "STRING_TOO_LARGE". Anyway, by just removing this line here, I can draw the vector perfectly well (while it is still 250 kb, but now incomplete).
What's quite surprising is that Android Studio only displays a warning (where the string is longer than 32k characters) just to make me understand that an svg with a long path can be slow to display, instead of warning me that the path will be corrupted because it's too long (or even better turn this warning into an error in the IDE, to avoid an unpleasant surprise).
Thanks to #TheBigBadBoy I splitted long SVG path into several segments. I opened XML file of the image and divided the path by M symbol (see also how to split one path into two paths in svg).
<path
android:fillColor="#ffffff"
android:fillType="evenOdd"
android:pathData="M40.255,47.8897C40.1686, ... ,47.8897ZM38.7713,40.2642L38.8362, ..., 46.0978Z"
/>
You can see that each segment starts with M and ends with Z. You can retain about 20Kb in the first path and cut the tail. Then paste the tail into the second path. Make so many paths that all of them become less than 32 Kb.
So, you will get:
<path
android:fillColor="#ffffff"
android:fillType="evenOdd"
android:pathData="M40.255,47.8897C40.1686, ... ,47.8897Z"
/>
<path
android:fillColor="#ffffff"
android:fillType="evenOdd"
android:pathData="M38.7713,40.2642L38.8362, ..., 46.0978Z"
/>
I am using vector drawables programmatically instead of png like :
getSupportActionBar().setHomeAsUpIndicator(getResources().getDrawable(R.drawable.ic_navigate_before_black_24dp));
Everything is working fine but the app crashes in android 4.2 & below. Am getting the following log
Caused by: android.content.res.Resources$NotFoundException:
at android.content.res.Resources.loadDrawable (Resources.java:2842)
at android.content.res.Resources.getDrawable (Resources.java:1521)
at android.app.Activity.performCreate (Activity.java:5326)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2218)
Caused by: org.xmlpull.v1.XmlPullParserException:
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)
I am using gradle 3.0.1 and also added
vectorDrawables.useSupportLibrary = true
still no help.
Check vector drawable <path>. Sometimes this happens when <path> tag in XML file is too long.
Try to move your image from drawable to drawable-nodpi.
UPDATE
Try to use AppCompatResources.getDrawable(view.getContext(), id); when getting drawable
I'm getting a Resources$notfoundexception on older (pre-L) devices. I'm including the full stacktrace below.
My version of the support library is the latest (24.1.0), and I've included in my gradle file the line:
vectorDrawables.useSupportLibrary = true
My base theme is a noactionbar theme - "Theme.AppCompat.Light.NoActionBar"
The crash is happening on this line in my code where I reference the back arrow in a support-toolbar in order to later change it's color:
#SuppressLint("PrivateResource") final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_material);
What could be the cause of this? This code works fine for all users with L or above.
07-19 22:36:57.029 9330-9330/mypkg E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {mypkg/mypkg.activites.myActivity}: android.content.res.Resources$NotFoundException: File res/drawable/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020013
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2619)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
at android.app.ActivityThread.access$600(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4929)
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:798)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020013
at android.content.res.Resources.loadDrawable(Resources.java:1957)
at android.content.res.Resources.getDrawable(Resources.java:673)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:354)
at mypkg.base.mymethod(myactivity.java:100)
at mypkg.mymethod(myactivity.java:100)
at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:511)
at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:178)
at android.app.Activity.performResume(Activity.java:5341)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
at android.app.ActivityThread.access$600(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4929)
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:798)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
at dalvik.system.NativeStart.main(Native Method)
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #17: invalid drawable tag vector
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:877)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
at android.content.res.Resources.loadDrawable(Resources.java:1954)
at android.content.res.Resources.getDrawable(Resources.java:673)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:354)
at mypkg/mymethod(myactivity.java:100)
at mypkg/mymethod(myactivity.java:100)
at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:511)
at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:178)
at android.app.Activity.performResume(Activity.java:5341)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
at android.app.ActivityThread.access$600(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4929)
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:798)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
at dalvik.system.NativeStart.main(Native Method)
The answer to this turned out to be buried at the bottom of this guide:
https://medium.com/#chrisbanes/appcompat-v23-2-age-of-the-vectors-91cbafa87c88#.xucjbsts0
It turns out that all you need to add this line in at the beginning of the activity that will use the resource:
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
Please make sure you are using AppCompatActivity instead Activity. If you're using AppCompat's theme, then you also need to use it's Activity.
Another solution,
in addition to AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
is to wrap your vector drawable into another drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_your_vector"/>
</selector>
Might be useful when you use as a drawable for a TextView (i.e. DrawableLeft)
Accepted answer is not covered all cases. It will not work on Android 4.0.3/4.1.1/4.1.2 platform with 25.x.x support library. The right way to fix problem with abc_ic_ab_back_material.xml is to override homeAsUpIndicator attribute in your theme. For example, my theme is inherited from Theme.AppCompat.Light.NoActionBar. As for value of mentioned attribute, you can use #drawable/abc_ic_ab_back_mtrl_am_alpha or your custom 'back' drawable.
private resources its treated differently at compile time and runtime..to fix I usually take the offending private resource and backport it to my app in my res files
I was using the application context when calling ContextCompat.getDrawable() which also crashes the app with Resources$NotFoundException and now the following message even though everything else was set up just fine:
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.
All I had to do was to change to the view's Context. :)
Instead of:
ContextCompat.getdrawable()
Try using:
AppCompatDrawableManager.get().getDrawable()
This question already has answers here:
Update Android Support Library to 23.2.0 cause error: XmlPullParserException Binary XML file line #17<vector> tag requires viewportWidth > 0
(31 answers)
Closed 6 years ago.
Recently android support library was updated to 23.2.0. After downloading android sdk and updating android design support library into 23.2.0, this error happens repeatedly. My project can't even be compiled. The complete error log says:
03-02 12:00:04.945 9324-9324/com.creditease.zhiwang.debug E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.creditease.zhiwang.debug/com.creditease.zhiwang.activity.TabContainerActivity}: android.content.res.Resources$NotFoundException: File res/drawable/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020016
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$700(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5528)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020016
at android.content.res.Resources.loadDrawable(Resources.java:2974)
at android.content.res.Resources.getDrawable(Resources.java:1558)
at android.support.v7.widget.TintResources.superGetDrawable(TintResources.java:48)
at android.support.v7.widget.AppCompatDrawableManager.onDrawableLoadedFromResources(AppCompatDrawableManager.java:374)
at android.support.v7.widget.TintResources.getDrawable(TintResources.java:44)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:323)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:180)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:173)
at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:184)
at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:91)
at android.support.v7.app.ToolbarActionBar.<init>(ToolbarActionBar.java:74)
at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:210)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:119)
at com.creditease.zhiwang.activity.BaseActivity.initToolBar(BaseActivity.java:300)
at com.creditease.zhiwang.activity.BaseActivity.initToolBar(BaseActivity.java:265)
at com.creditease.zhiwang.activity.TabContainerActivity.onCreate(TabContainerActivity.java:107)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2271)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$700(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5528)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
at dalvik.system.NativeStart.main(Native Method)
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #17: invalid drawable tag vector
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:933)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:873)
at android.content.res.Resources.loadDrawable(Resources.java:2970)
at android.content.res.Resources.getDrawable(Resources.java:1558)
at android.support.v7.widget.TintResources.superGetDrawable(TintResources.java:48)
at android.support.v7.widget.AppCompatDrawableManager.onDrawableLoadedFromResources(AppCompatDrawableManager.java:374)
at android.support.v7.widget.TintResources.getDrawable(TintResources.java:44)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:323)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:180)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:173)
at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:184)
at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:91)
at android.support.v7.app.ToolbarActionBar.<init>(ToolbarActionBar.java:74)
at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:210)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:119)
at com.creditease.zhiwang.activity.BaseActivity.initToolBar(BaseActivity.java:300)
at com.creditease.zhiwang.activity.BaseActivity.initToolBar(BaseActivity.java:265)
at com.creditease.zhiwang.activity.TabContainerActivity.onCreate(TabContainerActivity.java:107)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2271)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$700(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5528)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
at dalvik.system.NativeStart.main(Native Method)
This error was thrown by setSupportActionBar(toolbar); whereas it didn't happen at 23.0.1 of android design library 23.2.0. Meanwhile according this log, I guessed this drawable was removed since android design library 23.2.0.
So, could someone told me why is this happening?
I think you need to make changes in your gradle.
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
You’ll note this new attribute only exists in the version 2.0 of the Gradle Plugin. If you are using Gradle 1.5 you’ll instead use
// Gradle Plugin 1.5
android {
defaultConfig {
// Stops the Gradle plugin's automatic rasterization of vectors
generatedDensities = []
}
// Flag to tell aapt to keep the attribute ids around
// This is handled for you by the 2.0+ Gradle Plugin
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
I found similar question here.
See Support Vector Drawables and Animated Vector Drawables in Android Support Library update.
I hope its help you.
As per as the documentation of Google's support library for 24.0.0, they have changed the vector drawable library to what it was before:
Added AppCompatDelegate.setCompatVectorFromResourcesEnabled() method to re-enable usage of vector drawables in DrawableContainer objects on devices running Android 4.4 (API level 19) and lower. See AppCompat v23.2 — Age of the vectors! for more information.
I faced the same issue and my SVG statelist drawables used in my project were working just fine till Marshmallow devices.
Later when I got the crash for the same in Android N, I realized that the svgs were a bit corrupted and contained characters like: � and this caused the crash.
But these were not reflected in Android Marshmallow and prior devices.
Make sure your vector drawable doesn't contain any of those characters as the way of parsing has been changed from the library 24.0.0. So vector drawables working fine till Marshmallow might not work in Nougat devices.
Hope this helps :)
I solve this problem by updating my support library from
'com.android.support:appcompat-v7:23.2.0'
'com.android.support:design:23.2.0'
to the same dependencies of 23.2.1.
When I met the problem, I had not made any changes in my module built by Android Studio.
So I was so confused then I tried to update android support library.
After updating, please remember to sync your build.gradle
I solved the issue as follows:
Try with changing styles.xml to
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
This is because if style requires ActionBar there are chances of not finding abc_back button but with no action bar problem is solved
This worked for me: Replace the com.android.support:design version in build.gradle with one that works. Find which version works by creating a new project from scratch in Android Studio and using the version from that.
I had this problem when I added a Navigation Drawer Activity from the File->New->Activity menu to an older project with Android Studio.
Android Studio added a dependency like so:
compile 'com.android.support:design:24.0.0-alpha1'
(I'm not sure of the exact version but it had '24' and 'alpha').
I then created a new dummy project, specifying a Navigation Drawer Activity in the new project wizard. I noticed that the new project had a different dependency: compile 'com.android.support:design:23.2.1'
So I took this dependency and put it in the first project, and the problem was solved.
Source - http://android-developers.blogspot.in/2016/02/android-support-library-232.html
when using AppCompat with ImageView (or subclasses such as ImageButton
and FloatingActionButton), you’ll be able to use the new app:srcCompat
attribute to reference vector drawables (as well as any other drawable
available to android:src):
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_add" />
where you can define your app in your root element as
xmlns:app="http://schemas.android.com/apk/res-auto"
I've had this issue because my Manifest file had style that was defined only in v21 style. Not sure why it was pointing at this type of error but someone may find this helpful.
I'm using CardView in my project and I'm getting an error on older devices:
E android.view.InflateException: Binary XML file line #25: Error inflating class android.support.v7.widget.CardView
E at android.view.LayoutInflater.createView(LayoutInflater.java:518)
E at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
E at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
E at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
E at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
E at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
Seems similar to a few questions on SO such as this one
My problem is that the solution for that questions feels very hacky and that's understandable as the answer was given last year when Lollipop just entered the public preview. Have things changed since? How do I get the resources?
I've tried a few things such as importing .aar as a module but nothing seems to work. I've followed the official documentation on how to add support libraries (https://developer.android.com/tools/support-library/setup.html#libs-with-res) but that didn't work. Also according to that link - there doesn't seem to be any difference between adding libraries with and without resources for Android Studio?
My app build.gradle includes the following dependencies:
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:cardview-v7:21.0.3'
compile 'com.android.support:recyclerview-v7:21.0.3'
CardView layout is used here
<android.support.v7.widget.CardView
xmlns:card="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
card:cardCornerRadius="6dp"
card:cardUseCompatPadding="true"
tools:ignore="NewApi">
EDIT: the problem noticed on Samsung S2 2.3 - API 19+ works fine
?android:attr/selectableItemBackground is platform related, use it without android: prefix to refer to AppCompat's attribute.