我已经遍布堆栈溢出,毫无疑问能够解决我的问题。
我试图在使用API 15的设备上播放动画矢量,但我不断获得以下运行时异常:
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)
我使用上一个问题中您可以在此处找到的相同资源Animated Vector Drawable not working with API 15
此外,我已经测试过,而不是使用ImageView来使用AppCompatImageView,但仍然是同样的错误。
我一直在调试此错误,但在以下声明中失败:
final XmlPullParser parser = res.getXml(resId);
内部:
public static VectorDrawableCompat create(@NonNull Resources res, @DrawableRes int resId, @Nullable Theme theme)
VectorDrawableCompat verion 23.3.0的方法。
我似乎遇到兼容性问题。在某些时候,Android决定使用API 15的XML解析而不是使用compat版本。关于如何解决这个恼人问题的任何想法?
更新 我跟着Chris Banes博客,但仍然没有运气。基本上我已经将compat库更新为23.4.0并在我的活动中使用了以下内容:
static {
AppCompatDelegate.setCompatVectorFromSourcesEnabled(true);
}
答案 0 :(得分:1)
有支持库25.4.0,它旨在解决我们所有的问题。例如,它最终支持路径变形! p>
要使用它,请添加:
allprojects {
repositories {
maven {
url "https://maven.google.com"
}
}
}
在项目gradle文件中,相应依赖项本身:
compile 'com.android.support:support-v4:25.4.0'
之后,您可以使用带有路径变形的矢量可绘制动画(通常是背后的罪魁祸首&#34;无效的可绘制标签动画矢量&#34;)以下方式(一点Kotlin代码):
val drawable = AnimatedVectorDrawableCompat.create(this, R.drawable.vector_drawable)
imageView.setImageDrawable(drawable) // You may use usual ImageView class, no Compat necessary.
drawable.start()
答案 1 :(得分:0)
`VectorDrawable`
和`AnimatedVectorDrawable`
可以通过这种方式充气:
getDrawable()
方法://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);
如果在java代码中膨胀Drawable,建议始终使用ResourcesCompat.getDrawable()
,因为这会在适用时处理Lollipop后备。这允许系统缓存Drawable ConstantState,因此效率更高。
该库具有以下变形(双向)动画:
API 16
电话上制作了上述图片:
import com.wnafee.vector.compat.AnimatedVectorDrawable;
mdrawable = (AnimatedVectorDrawable) AnimatedVectorDrawable.getDrawable(this.getApplicationContext(), R.drawable.consolidated_animated_vector);
在此处查看vector-compat
的github 自述文件:https://github.com/wnafee/vector-compat
如果您将其与您的应用模块的API 14
build.gradle
合并(通常位于文件末尾),这将解决您的问题(降至dependencies
):
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'
}