Android ResourceNotFound 当使用矢量 xml 作为 drawable API 16
Android ResourceNotFound when using vector xml as drawable API 16
嗨,我正在尝试在我的应用程序中使用 xml vecotr 作为可绘制对象,并且在 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.access0(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.access0(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.access0(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>
Gradle:
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'
只需将 false 应用于 vectorDrawables.useSupportLibrary 即可。像这样:-
vectorDrawables.useSupportLibrary = false
原因是 Vector drawable 不适用于棒棒糖以下的 api。
它有一些问题。但是你可以声明矢量可绘制 & 并且可以使用下面的棒棒糖版本但是你必须将 useSupportLibrary 设置为 false 这样 android studio 将为你创建 png 文件:)
嗨,我正在尝试在我的应用程序中使用 xml vecotr 作为可绘制对象,并且在 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.access0(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.access0(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.access0(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>
Gradle:
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'
只需将 false 应用于 vectorDrawables.useSupportLibrary 即可。像这样:-
vectorDrawables.useSupportLibrary = false
原因是 Vector drawable 不适用于棒棒糖以下的 api。 它有一些问题。但是你可以声明矢量可绘制 & 并且可以使用下面的棒棒糖版本但是你必须将 useSupportLibrary 设置为 false 这样 android studio 将为你创建 png 文件:)