首页 > 解决方案 > Android:在将 drawable 添加到 styles.xml 时出现 Resources$NotFoundException

问题描述

我有这个splash_screen.xml

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

    <item
        android:gravity="center"
        android:src="@mipmap/ic_launcher"/>
</layer-list>

然后我想将其添加<item>styles.xml

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_screen</item>
</style>

但这会引发:

Caused by: android.content.res.Resources$NotFoundException: File res/drawable/splash_screen.xml from drawable resource ID #0x7f070099

当我尝试:

<item name="android:windowBackground">@color/colorPrimary</item>

我的代码工作正常,并在 colorPrimary 中显示一个屏幕。

我该如何解决?

我尝试重建、清理、重新启动 Android Studio 以及 SO 上的每个帖子。没有任何帮助。

编辑

splash_screen.xml肯定在drawable文件夹中

在此处输入图像描述

Android Studio 建议使用该文件。

我的可绘制文件夹:

在此处输入图像描述

标签: androidfileexceptionsplash-screenfilenotfoundexception

解决方案


您必须像在第一个中一样在您的项目标签android:drawable中使用。android:src

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

    <item
        android:gravity="center"
        android:drawable="@mipmap/ic_launcher"/>
</layer-list>

除此之外,始终发布异常的完整堆栈跟踪。实际异常清楚地表明了如下问题:

原因:org.xmlpull.v1.XmlPullParserException:二进制 XML 文件第 6 行:标签需要“drawable”属性或定义可绘制对象的子标签


推荐阅读