矢量可绘制对象在 XML 中未加载 ResourceNotFoundException。

huangapple go评论72阅读模式
英文:

Vector drawables not loading in xml ResourceNotFoundException

问题

我在加载矢量文件时在我的闪屏背景 XML 文件中遇到了问题。矢量文件已经成功加载,因为我可以在 Android Studio 中打开矢量文件,并且在 XML 文件的一侧获得矢量图形的小图标。然而,XML 文件的设计视图显示矢量文件存在错误,并且在尝试运行我的应用程序时出现 ResourceNotFoundException 错误。

以下是代码:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque">
    <item android:drawable="@color/colorPrimary"/>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/ic_loading_screen_logo"/>
    </item>
</layer-list>

[闪屏背景.xml][1]

[1]: https://i.stack.imgur.com/nbxvJ.jpg

我的 Gradle 版本是 `classpath 'com.android.tools.build:gradle:4.0.1'`

我在 Gradle 应用程序文件中添加了以下行 `vectorDrawables.useSupportLibrary = true`

似乎我使用的任何矢量文件都有这个错误,因此可能缺少某种设置。

请以任何方式协助,谢谢!
英文:

I am having trouble with loading vector files in my splash background xml file. The vector has been loaded successfully because I can open the vector file in android studio and I get a small graphic of the vector on the side of the xml file. However, the design view of the xml file shows an error for the vector file, and I get a ResourceNotFoundException when trying to run my app.

Code below:

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;layer-list xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;

    android:opacity=&quot;opaque&quot;&gt;
    &lt;item android:drawable=&quot;@color/colorPrimary&quot;/&gt;
    &lt;item&gt;
        &lt;bitmap
            android:gravity=&quot;center&quot;
            android:src=&quot;@drawable/ic_loading_screen_logo&quot;/&gt;
    &lt;/item&gt;

&lt;/layer-list&gt;

splash background.xml

My grade version is classpath &#39;com.android.tools.build:gradle:4.0.1&#39;

and I have included the following line in my grade app file vectorDrawables.useSupportLibrary = true

It seems that any vector file that I use has this error, so there is some setting of some sort that is missing.

Please assist in anyway.

Thanks,

答案1

得分: 0

问题是将xml可绘制对象包装在一个<bitmap/>中。

尝试这样做:

将以下部分替换为:

    <item android:gravity="center" 
          android:drawable="@drawable/ic_loading_screen_logo"/>
英文:

The problem is wrapping the xml drawable in a &lt;bitmap/&gt;

Try this:

Replace

&lt;item&gt;
        &lt;bitmap
            android:gravity=&quot;center&quot;
            android:src=&quot;@drawable/ic_loading_screen_logo&quot;/&gt;
&lt;/item&gt;

With:

    &lt;item android:gravity=&quot;center&quot; 
          android:drawable=&quot;@drawable/ic_loading_screen_logo&quot;/&gt;

答案2

得分: 0

Sure, here's the translation:

  1. &lt;bitmap 替换为 &lt;item
  2. 确保你的矢量图像位于 drawable 文件夹中,而不是 drawable-24
  3. 检查矢量图像的 path,也许它太长了。
英文:
  1. replace &lt;bitmap to &lt;item
  2. make sure that your vector is in drawable folder not in drawable-24.
  3. check vectory path may be its too long.

huangapple
  • 本文由 发表于 2020年9月16日 16:58:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63916592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定