Error with vector images

1

I have arrangements of vector images but when I run the app, it is truncated, what the phone displays is "Unfortunately, app has stopped" and what the logcat shows is:

  

03-29 01: 28: 37.177 3176-3176 / matgic.com.matgic E / VdcInflateDelegate: Exception while inflating                                                                        android.content.res.Resources $ NotFoundException: File res / drawable-v22 / $ ic_strawberry__0.xml from color state list resource ID # 0x7f070023   Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line # 0: invalid color state list tag gradient

here the code:

int [] f2={R.drawable.ic_apple, R.drawable.ic_pineapple, R.drawable.ic_strawberry, R.drawable.ic_coconut};
    img1.setImageResource(R.drawable.ic_strawberry);

build.gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
defaultConfig {
    applicationId "matgic.com.matgic"
    minSdkVersion 14
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    vectorDrawables.useSupportLibrary = true
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

aaptOptions {
    additionalParameters "--no-version-vectors"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}


dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.volley:volley:1.0.0'
//compile 'com.android.support:design:24.+'
compile 'com.android.support:design:26.1.0'
compile 'com.getbase:floatingactionbutton:1.10.1'
testCompile 'junit:junit:4.12'

}
    
asked by Sergio 29.03.2018 в 07:51
source

1 answer

0

The android: fillColor property of a Gradient is supported by API 24+ ( 7.0 or later)

  

android: fillColor Specify the color used to complete   the route. It can be a color or, for SDK 24+, a status list of   color or a color gradient (see GradientColor and   GradientColorItem). If this property is animated, any value   set by the animation will override the original value. It is not drawn   no route padding if this property is not specified.

If you test your code on a device with OS 7.0 or later, you would not have any problem but in this case to work in previous versions add in the folder /drawable the version of your graphic without gradients, and create a folder /drawable-v24 where you add the complete .xml with the gradients.

The /drawable-v24 chart would be loaded only for Android versions 7.0 or later:

The solution is to comment or eliminate the gradients, for example:

<vector android:height="24dp" android:viewportHeight="651.95"
    android:viewportWidth="531.48" android:width="24dp"
    xmlns:aapt="http://schemas.android.com/aapt" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:pathData="M386.8,30.2c-48.5,0 -76.1,18 -121.1,18s-72.6,-18 -121.1,-18c-87.9,0 -144.7,83.3 -144.7,186 0,92.9 160,350.5 265.7,350.5 112.9,0 265.7,-257.6 265.7,-350.5C531.5,113.5 474.7,30.2 386.8,30.2Z">
        <!--<aapt:attr name="android:fillColor">
            <gradient android:endX="212457.73219299316"
                android:endY="440836.2612554932"
                android:startX="212457.73219299316"
                android:startY="92857.94223999024" android:type="linear">
                <item android:color="#FFFC3A11" android:offset="0.0"/>
                <item android:color="#FFDA0300" android:offset="1.0"/>
            </gradient>
        </aapt:attr>-->
    </path>
    ...
    ...

and use the android: fillColor property of the path to assign a color, for example the color red:

<path android:fillColor="@android:color/holo_red_light" android:pathData="M386.8,30.2c-48.5,0 -76.1,18 -121.1,18s-72.6,-18 -121.1,-18c-87.9,0 -144.7,83.3 -144.7,186 0,92.9 160,350.5 265.7,350.5 112.9,0 265.7,-257.6 265.7,-350.5C531.5,113.5 474.7,30.2 386.8,30.2Z">
...
...

and the green color for the leaves:

   <path android:fillColor="@android:color/holo_green_light"  android:pathData="M179.7,1.2c-39,-0.9 -73.5,36.7 -78.1,70 65.4,15.4 111.7,-19.6 111.7,-19.6 -12,26.3 -3,49.5 9.7,81.6 9.7,24.5 6.3,43.5 6.3,43.5S346.7,169.4 327.6,61.8c30,28.2 73.5,37.5 101.7,27.3 -8.5,-24.8 -5.8,-63.8 -47.5,-80C345.9,-4.8 302.8,-4.8 276.2,23.4 249.6,2.1 179.7,1.2 179.7,1.2Z">
   ...
   ...

This version of your chart would be saved within /drawable to get the gradient-free image supported for versions prior to Android 7.0:

by the way aaptOptions must be declared inside the block android , this in your build.gradle :

android {

    aaptOptions {
        additionalParameters "--no-version-vectors"
    }

}
    
answered by 29.03.2018 / 18:03
source