gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:58:08 25 4
gpt4 key购买 nike

嗨,我尝试在我的应用程序中使用 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.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>

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

原因是 Vectordrawable 不适用于 Lollipop 以下的 api。它有一些问题。但是你可以声明矢量可绘制 & 并可以使用下面的 Lollipop 版本,但你必须将 useSupportLibrary 设置为 false,以便 android studio 将为你创建 png 文件:)

关于使用矢量 xml 作为可绘制 API 16 时的 Android ResourceNotFound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44020903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com