Android Nougat无法显示某些矢量文件(Resources $ NotFoundException) [英] Android Nougat cannot show certain vector files (Resources$NotFoundException)

查看:165
本文介绍了Android Nougat无法显示某些矢量文件(Resources $ NotFoundException)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发包含许多矢量图形的应用程序.最近,我开始在Nougat上进行测试,发现它立即崩溃了.日志猫在加载向量时会显示Resources $ NotFoundException,这让人想起带有向量图形的Android早期版本中的所有AppCompat错误.但是,它在KitKat,Lollipop和棉花糖上运行良好.只有在牛轧糖上才出现此错误.

I have been working on an app that contains a number of vector graphics. Recently I started testing it on Nougat and found it crashing immediately. The logcat shows a Resources$NotFoundException while loading a vector which is reminiscent of all the AppCompat errors in earlier versions of Android with vector graphics. However, it runs fine on KitKat, Lollipop, and Marshmallow. It is only on Nougat that I am getting this error.

我将其范围缩小到一定数量的引起异常的矢量文件,但是我看不到它们为什么与我使用的其他文件不同.所有这些都是通过以下相当简单的SVG生成的: http://inloop.github.io/svg2android/

I have narrowed it down to a certain number of vector files that cause the exception, but I cannot see why they are different than the others I am using. All were generated from fairly simple SVGs from: http://inloop.github.io/svg2android/

我的猜测是,它们从矢量创建可绘制对象的方式发生了某些变化,并且由于某些原因,这些文件现在导致错误,因此无法创建可绘制对象.当库开始将可绘制对象显示在屏幕上时,它无法找到"缺少的可绘制对象,因为它尚未创建.我不确定在牛轧糖的矢量文件上要进行哪些额外的检查/限制.

My guess is that something has changed in the way they are creating drawables from vectors and for some reason these files are now causing an error so that the drawable is not created. When the library gets to putting the drawable on screen it cannot "find" the missing drawable because it has not been created. I am not sure what the extra check/restriction is on the vector file in Nougat.

还有其他人经历过吗?知道为什么吗?牛轧糖的向量处理发生了什么变化?

Has anyone else experienced this? Any idea why? What has changed with Nougat's vector handling?

遇到错误并搜索其他Vector错误后,我将gradle设置更新为:

After experiencing the error and searching through other Vector errors, I updated my gradle settings to:

  • buildToolsVersion '25 .0.3'
  • vectorDrawables.useSupportLibrary = true
  • 编译'com.android.support:appcompat-v7:25.4.0'
  • 编译'com.android.support:preference-v14:25.4.0'
  • 编译'com.android.support:recyclerview-v7:25.4.0'
  • 编译'com.android.support:design:25.4.0'

我也包括在主要班级中:

I have also included in the main class:

static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

日志猫:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.MyApp.Application.MyAppApplication, PID: 4957
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.MyApp.Application.MyAppApplication/com.MyApp.Application.MyAppApplication.MyApp}: android.content.res.Resources$NotFoundException: Drawable com.MyApp.Application.MyAppApplication:drawable/icon_month with resource ID #0x7f0800da
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                   Caused by: android.content.res.Resources$NotFoundException: Drawable com.MyApp.Application.MyAppApplication:drawable/icon_month with resource ID #0x7f0800da
                   Caused by: android.content.res.Resources$NotFoundException: File res/drawable/icon_month.xml from drawable resource ID #0x7f0800da
    at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:725)
    at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571)
    at android.content.res.Resources.getDrawable(Resources.java:767)
    at android.content.Context.getDrawable(Context.java:525)
    at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:30)
    at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:372)
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
    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:92)
    at com.MyApp.Application.MyAppApplication.MyApp.onCreate(MyApp.java:233)
    at android.app.Activity.performCreate(Activity.java:6662)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                   Caused by: java.lang.IllegalArgumentException: Path string cannot be empty.
    at android.util.PathParser.nCreatePathDataFromString(Native Method)
    at android.util.PathParser.-wrap1(PathParser.java)
    at android.util.PathParser$PathData.<init>(PathParser.java:74)
    at android.graphics.drawable.VectorDrawable$VFullPath.updateStateFromTypedArray(VectorDrawable.java:1556)
    at android.graphics.drawable.VectorDrawable$VFullPath.inflate(VectorDrawable.java:1507)
    at android.graphics.drawable.VectorDrawable.inflateChildElements(VectorDrawable.java:693)
    at android.graphics.drawable.VectorDrawable.inflate(VectorDrawable.java:598)
    at android.graphics.drawable.DrawableInflater.inflateFromXml(DrawableInflater.java:130)
    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1224)
    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1197)
E/AndroidRuntime:     at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:715)
        ... 23 more

XML向量文件的开头(无法发布整个文件):

The beginning of the XML vector file (unable to post whole file):

    <?xml version="1.0" encoding="utf-8"?>
    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="100.000000dp"
        android:height="75.666667dp"
        android:viewportWidth="100.000000"
        android:viewportHeight="75.666667">

        <group
                android:translateY="75.666667"
                android:scaleX="0.003333"
                android:scaleY="-0.003333">
            <path
                android:fillColor="#000000"
                android:strokeWidth="1"
                android:pathData="M28898 22695 c-7 -7 -376 -19 -491 -16 -69 2 -113 -1 -122 -9 -14 -12 -296 -34
    -330 -26 -18 4 -170 7 -235 4 -14 0 -47 3 -75 7 -61 9 -223 9 -265 0 -19 -4 -36 -2
    -46 6 -12 10 -20 9 -39 -1 -20 -11 -35 -11 -77 -1 -38 9 -151 11 -404 8 -192 -2
    -354 -6 -360 -9 -5 -3 -103 -4 -217 -3 -115 1 -210 -2 -213 -7 -3 -5 -18 -6 -34 -3
    -23 5 -126 7 -310 5 -14 -1 -185 -1 -380 -1 -280 0 -567 -3 -705 -8 -5 -1 -35 1
    -65 3 -100 8 -183 7 -324 -2 -77 -5 -148 -7 -158 -4 -10 2 -18 -1 -18 -7 0 -14 -35
    -14 -76 1 -25 9 -39 9 -56 0 -13 -6 -81 -14 -153 -17 -71 -3 -195 -9 -275 -14 -80
    -5 -152 -10 -160 -10 -138 -6 -171 -10 -184 -24 -9 -9 -16 -20 -16 -26 0 -6 -4 -11
    -10 -11 -15 0 -12 21 7 42 16 18 14 19 -68 19 -121 0 -114 0 -234 -5 -113 -6 -236
    -11 -550 -22 -221 -8 -229 -9 -243 -18 -7 -4 -80 -7 -162 -8 -85 0 -153 -4 -156 -9
    -4 -6 -10 -6 -18 0 -6 5 -68 11 -136 11 -114 1 -181 -1 -371 -14 -36 -3 

....

谢谢!

修改 似乎问题出在具有更长"pathData"条目的矢量文件中(路径数似乎不是问题).当在更长的pathData条目上调用本机JNI函数GetStringUTFChars时,似乎发生了错误,并且它返回了解析器无法绘制的空字符串.我不知道这是否只是长度问题,也不知道为什么在AppCompat库中可以正常工作.

Edit It seems like the issue is vector files with longer "pathData" entries (Number of paths does not seem to be the issue). The error seems to happen when the native JNI function GetStringUTFChars is called on a longer pathData entry and it returns an empty string which the parser cannot draw. I do not know if this is just a length thing nor why this works fine in the AppCompat libraries.

当前的解决方法是1)对具有longpathData的任何向量使用位图,或2)修改xml文件并将pathData拆分为多个路径(感谢Lewis McGreary的建议!).

Current workarounds are 1) to use bitmaps for any vectors with long pathData or 2) to modify the xml files and split the pathData up into multiple paths (Thanks Lewis McGreary for the suggestion!).

两者都需要测试每个矢量图形以确定哪些工作,然后分别修改可绘制对象.这是不理想的,特别是对于从大型池中选择图像的特定应用程序而言.完全移动到png是一种选择,但会导致apk较大且缩放比例较差:(

Both require testing every vector graphic to determine which work and then individually modifying the drawables. This is not-ideal, especially for this particular app which chooses images from a large pool. Moving wholly to pngs is an option, but results in a larger apk and poorer scaling :(

与崩溃相关的Android源:

Android source related to crash:

PathParser Java

PathParser JNI-这是对GetStringUTFChars

PathParser JNI Hui-这是发生崩溃

推荐答案

我找到了解决方法!看来问题出在AAPT2中.

I found a workaround! It seems the problem is in AAPT2.

添加:

android.enableAapt2 = false

收件人

gradle.properties

这篇关于Android Nougat无法显示某些矢量文件(Resources $ NotFoundException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆