android.content.res.Resources$NotFoundException:文件 res/drawable-v21/launch_background.xml

Posted

技术标签:

【中文标题】android.content.res.Resources$NotFoundException:文件 res/drawable-v21/launch_background.xml【英文标题】:android.content.res.Resources$NotFoundException: File res/drawable-v21/launch_background.xml 【发布时间】:2021-03-16 09:22:38 【问题描述】:

之前我正在构建和运行一个 Flutter 应用程序。我不记得做过任何更改,但最近,我在通过 android Studio 在智能手机上构建和运行应用程序时遇到了此类错误:

Launching lib\main.dart on SM N900 in debug mode...
Running Gradle task 'assembleDebug'...
E/AndroidRuntime(12731): FATAL EXCEPTION: main
E/AndroidRuntime(12731): Process: io.flutter.demo.gallery, PID: 12731
E/AndroidRuntime(12731): java.lang.RuntimeException: Unable to start activity ComponentInfoio.flutter.demo.gallery/io.flutter.demo.gallery.MainActivity: android.content.res.Resources$NotFoundException: File res/drawable-v21/launch_background.xml from drawable resource ID #0x7f040000
E/AndroidRuntime(12731):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
E/AndroidRuntime(12731):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
E/AndroidRuntime(12731):    at android.app.ActivityThread.access$900(ActivityThread.java:177)
E/AndroidRuntime(12731):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
E/AndroidRuntime(12731):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(12731):    at android.os.Looper.loop(Looper.java:145)
E/AndroidRuntime(12731):    at android.app.ActivityThread.main(ActivityThread.java:5942)
E/AndroidRuntime(12731):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(12731):    at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(12731):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
E/AndroidRuntime(12731):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
E/AndroidRuntime(12731): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-v21/launch_background.xml from drawable resource ID #0x7f040000
E/AndroidRuntime(12731):    at android.content.res.Resources.loadDrawableForCookie(Resources.java:3752)
E/AndroidRuntime(12731):    at android.content.res.Resources.loadDrawable(Resources.java:3620)
E/AndroidRuntime(12731):    at android.content.res.Resources.getDrawable(Resources.java:1852)
E/AndroidRuntime(12731):    at android.content.res.Resources.getDrawable(Resources.java:1818)
E/AndroidRuntime(12731):    at io.flutter.embedding.android.FlutterActivity.getSplashScreenFromManifest(FlutterActivity.java:495)
E/AndroidRuntime(12731):    at io.flutter.embedding.android.FlutterActivity.provideSplashScreen(FlutterActivity.java:469)
E/AndroidRuntime(12731):    at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onCreateView(FlutterActivityAndFragmentDelegate.java:301)
E/AndroidRuntime(12731):    at io.flutter.embedding.android.FlutterActivity.createFlutterView(FlutterActivity.java:520)
E/AndroidRuntime(12731):    at io.flutter.embedding.android.FlutterActivity.onCreate(FlutterActivity.java:414)
E/AndroidRuntime(12731):    at android.app.Activity.performCreate(Activity.java:6283)
E/AndroidRuntime(12731):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
E/AndroidRuntime(12731):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
E/AndroidRuntime(12731):    ... 10 more
E/AndroidRuntime(12731): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable
E/AndroidRuntime(12731):    at android.graphics.drawable.LayerDrawable.inflateLayers(LayerDrawable.java:202)
E/AndroidRuntime(12731):    at android.graphics.drawable.LayerDrawable.inflate(LayerDrawable.java:147)
E/AndroidRuntime(12731):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1150)
E/AndroidRuntime(12731):    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1063)
E/AndroidRuntime(12731):    at android.content.res.Resources.loadDrawableForCookie(Resources.java:3736)
E/AndroidRuntime(12731):    ... 21 more

颤振医生 flutter doctor -v 的输出为:

>flutter doctor -v
[√] Flutter (Channel master, 1.24.0-8.0.pre.1, on Microsoft Windows [Version 10.0.19041.264], locale en-US)
    • Flutter version 1.24.0-8.0.pre.1 at C:\Users\m3\repos\flutter
    • Framework revision e444b1e3fa (5 weeks ago), 2020-10-30 00:41:52 -0400
    • Engine revision 99cc50dfff
    • Dart version 2.11.0 (build 2.11.0-266.0.dev)

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
    • Android SDK at C:\Users\m3\AppData\Local\Android\sdk
    • Platform android-30, build-tools 30.0.1
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 4.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 50.0.1
    • Dart plugin version 193.7547
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] Connected device (3 available)
    • SM N900 (mobile) • 4d00b6ae472d80eb • android-arm    • Android 5.0 (API 21)
    • Web Server (web) • web-server       • web-javascript • Flutter Tools
    • Chrome (web)     • chrome           • web-javascript • Google Chrome 87.0.4280.66

• No issues found!

我只是简单地 Git checkout master 分支。打开安卓工作室。通过 USB 端口将 Android 智能手机设备连接到计算机。单击绿色三角形按钮 (Android Studio) 在装有 Android 5.0 的设备上构建和运行应用程序。在终端上抛出错误。我在 Windows 10 上。

注意 这些帖子表明可能需要对 Gradle 构建系统进行不同的配置,但不确定如何:

https://***.com/a/39419701/3405291

更新

Android Studio File &gt; Invalidate Caches / Restart... 没有解决问题。

更新

使用 Android 9 (API 28) 构建并运行应用,异常已解决,应用正常:

$ flutter doctor -v
[√] Flutter (Channel master, 1.26.0-2.0.pre.401, on Microsoft Windows [Version 10.0.19041.264], locale en-US)
    • Flutter version 1.26.0-2.0.pre.401 at C:\Users\m3\repos\flutter
    • Framework revision da5a454d5c (11 hours ago), 2021-01-17 01:24:03 -0500
    • Engine revision 609036f2bf
    • Dart version 2.12.0 (build 2.12.0-236.0.dev)

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
    • Android SDK at C:\Users\m3\AppData\Local\Android\sdk
    • Platform android-30, build-tools 30.0.1
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 4.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 50.0.1
    • Dart plugin version 193.7547
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] Connected device (2 available)
    • SM A105F (mobile) • RF8M60PVD5F • android-arm    • Android 9 (API 28)
    • Chrome (web)      • chrome      • web-javascript • Google Chrome 87.0.4280.88

• No issues found!

更新

到目前为止的含义是该应用在 Android 9 (API 28) 上运行良好,但在 Android 5.0 (API 21) 上抛出上述异常。有人知道问题可能是什么原因吗?我怎么可能在 Android 5.0 (API 21) 上运行该应用程序?

更新

这是相关的:

https://github.com/flutter/gallery/issues/383

更新

另外,这是相关的:

https://github.com/flutter/flutter/issues/73118

【问题讨论】:

Caused by: android.content.res.Resources$NotFoundException: File res/drawable-v21/launch_background.xml 【参考方案1】:

在文件android\app\src\main\res\drawable-v21\launch_background.xml中,可以通过替换解决异常

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="?android:colorBackground" />

通过

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white" />

【讨论】:

【参考方案2】:

在文件android/app/src/main/AndroidManifest.xml 中,通过注释掉这个异常来解决:

<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background" />

删除上面的解决了异常,虽然我不确定它是否有任何副作用。

【讨论】:

它有效,但如果您在 launch_background.xml 中有启动图像设置,则会删除它。那么我们如何修复它并保持启动图像显示? @Zenko 这个问题应该已经被this PR解决了! =) 首先,PR 在主频道。所以我尝试了主通道和稳定通道。两者都更新到最新版本。并尝试评论该部分而不是评论它。行为是相同的。如果我们不注释掉它,它就会崩溃,如果我们注释掉它,启动屏幕就不会显示。【参考方案3】:

在这里找到原始答案:Splash screen error: android.content.res.Resources$NotFoundException: Drawable

当您包含来自可绘制目录以外的目录的图像时,大多数情况下都会发生此问题。例如:

<item>
  <bitmap
       android:gravity="center"
       android:src="@mipmap/image" />
</item>

只需使用可绘制的图像,您将摆脱错误。

【讨论】:

【参考方案4】:

在我的情况下,解决此问题的方法是复制文件 launch_background.xml,其中指定了我的项目的启动图像和背景颜色,来自:

project\android\app\src\main\res\drawable

进入:

project\android\app\src\main\res\drawable-v21

【讨论】:

以上是关于android.content.res.Resources$NotFoundException:文件 res/drawable-v21/launch_background.xml的主要内容,如果未能解决你的问题,请参考以下文章