使用vector xml作为可绘制API 16时的Android ResourceNotFound

时间:2017-05-17 09:31:58

标签: android xml vector drawable xml-drawable

您好我试图在我的应用程序中使用xml vecotr作为drawable并且一切都在API 25中运行良好但是当我在API 16上尝试它时出现此错误。只有当我尝试以编程方式加载矢量时才会出现错误,当我在布局中使用它作为ImageView背景时我没有收到错误。

java.lang.RuntimeException: Unable to start activity ComponentInfo{.....Editor}: android.content.res.Resources$NotFoundException: File res/drawable/chevron_left.xml from drawable resource ID #0x7f02005c
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
                                                                       at android.app.ActivityThread.access$600(ActivityThread.java:130)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                       at android.os.Looper.loop(Looper.java:137)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                       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:786)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                       at dalvik.system.NativeStart.main(Native Method)
                                                                    Caused by: android.content.res.Resources$NotFoundException: File res/drawable/chevron_left.xml from drawable resource ID #0x7f02005c
                                                                       at android.content.res.Resources.loadDrawable(Resources.java:1923)
                                                                       at android.content.res.Resources.getDrawable(Resources.java:659)
                                                                       at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:374)
                                                                       at com...........Editor.onCreate(Editor.java:84)
                                                                       at android.app.Activity.performCreate(Activity.java:5008)
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                                                                       at android.app.ActivityThread.access$600(ActivityThread.java:130) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                       at android.os.Looper.loop(Looper.java:137) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:4745) 
                                                                       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:786) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                                                                       at dalvik.system.NativeStart.main(Native Method) 
                                                                    Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: 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:1920)
                                                                       at android.content.res.Resources.getDrawable(Resources.java:659) 
                                                                       at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:374) 
                                                                       at com...........Editor.onCreate(Editor.java:84) 
                                                                       at android.app.Activity.performCreate(Activity.java:5008) 
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                                                                       at android.app.ActivityThread.access$600(ActivityThread.java:130) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                       at android.os.Looper.loop(Looper.java:137) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:4745) 
                                                                       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:786) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                                                                       at dalvik.system.NativeStart.main(Native Method) 

并且在Editor.java中(扩展了AppCompatActivity)我尝试了这两个(两个都没有用):

    Drawable upArrow=getResources().getDrawable(R.drawable.chevron_left);
    Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.chevron_left).getCurrent();

矢量chevron_left.xml:

<!-- drawable/chevron_left.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="42dp"
    android:width="42dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <path android:fillColor="#fff" android:pathData="M15.41,16.58L10.83,12L15.41,7.41L14,6L8,12L14,18L15.41,16.58Z" />
</vector>

摇篮:

defaultConfig {
    applicationId "com....."
    minSdkVersion 16
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    vectorDrawables.useSupportLibrary = true
    multiDexEnabled true
}

compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:support-vector-drawable:25.3.1'
compile 'com.android.support:appcompat-v7:25.3.1'

1 个答案:

答案 0 :(得分:3)

将vectorDrawables.useSupportLibrary的false应用于false。像这样: -

vectorDrawables.useSupportLibrary = false

原因是Vector drawable不适用于棒棒糖下的apis。 它有一些问题。但是你可以声明vector drawable&amp;并且可以使用下面的棒棒糖版本,但你必须将useSupportLibrary设置为false所以android studio将为你创建png文件:)