2017-06-22 57 views
6

我一直在努力,它包含了许多矢量图形的应用程序。最近我开始在牛轧糖上进行测试,发现它立即崩溃。 logcat显示Resources $ NotFoundException,同时加载一个向量,该向量让人想起早期版本的Android中具有矢量图形的所有AppCompat错误。但是,它在KitKat,棒棒糖和棉花糖上运行良好。只是在牛轧糖上我得到了这个错误。

我已经把范围缩小到一定的数量,导致异常的矢量文件,但我不明白为什么他们比我现在用的是别人的不同。所有从相当简单SVGs从产生:http://inloop.github.io/svg2android/

我的猜测是,事情的方式发生了变化,他们正在创建载体,由于某种原因可绘制这样就不会产生提拉这些文件现在导致错误。当库在屏幕上放置drawable时,它不能“找到”缺少的drawable,因为它没有被创建。我不确定Nougat的矢量文件中额外的检查/限制是什么。

有没有其他人经历过这个?任何想法为什么?牛轧糖的矢量处理有什么变化?

遇到的错误,并通过其他的矢量误差搜索后,我更新了我的gradle这个设置:

  • buildToolsVersion '25 .0.3'
  • vectorDrawables.useSupportLibrary =真
  • 编译“com.android 。支持:程序兼容性-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'

我也包含在主类:

static { 
     AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); 
    } 

的logcat的:

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矢量文件的(无法发送整个文件):

<?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”条目矢量文件(似乎路径数不成为问题)。该错误似乎当本地JNI功能GetStringUTFChars被称为在一个较长的pathData进入发生,它将返回解析器不能得出一个空字符串。我不知道这是否只是一件长事,也不知道为什么这可以在AppCompat库中正常工作。

当前的解决方法是1)对于任何具有long pathData或2)的矢量使用位图来修改xml文件并将pathData分成多个路径(感谢Lewis McGreary的建议!)。

两者都需要测试每个矢量图形,以确定哪些工作,然后单独地修改所述图形内容。这是不理想的,尤其是对于从一个大型游泳池选择图像这一特定的应用程序。整体移动到PNG格式是一种选择,但将导致更大的APK和较差的比例:(

Android的源与崩溃:

PathParser Java

PathParser JNI - here is the call to GetStringUTFChars

PathParser JNI Hwui - here is where the crash happens

+1

我不知道真正原因是什么在这里,但我可以说使用支持库时 - 牛轧糖(API 24)和向上它委托给VectorDrawable的框架版本,并且API 23向下使用支持库版本。 –

+0

这有助于。更多的挖掘,它似乎是在更长的pathData字符串的24 + Api扼流圈,给出上面的“路径字符串不能为空”的错误。我一直无法找到矢量渲染的相应源代码。对于在Android Studio中使用内置Vector Studio创建的矢量图形也是如此。我只创建了一个新的牛轧糖,没有支持库,项目结果相同。很明显,支持库在渲染具有长路径数据的矢量方面做得更好,但实际上该做什么?测试牛轧糖上的所有载体?我可以强迫牛轧糖使用支持库吗? – Emilie

+2

我不确定是否可以强制使用支持库。如果更长的pathData是问题,并且根据特定图像,将单个路径分成多个较短的元素可能是一个解决方案。所以特别是如果图像在一个路径中有多个元素,通过“M”命令将是一个剪切/粘贴到新路径的好地方。 –

回答

2

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

添加:

android.enableAapt2 = false 

gradle.properties 
+0

这是否有任何副作用? – thailey01

+0

以上解决方案对我无效,因为发生'gradle sync'时出现错误。我想最好的解决方案是优化你的矢量绘制。可能SVGO会在一定程度上帮助https://github.com/svg/svgo – karthikk

0

我有没有其他解决方法比你提到的其他2个。但似乎这个问题已在Android 7.1中修复了

相关问题