不幸的是,Flutter App 停止工作并出现 Unable to start activity ComponentInfo 错误
Flutter App unfortunately stops working with Unable to start activity ComponentInfo error
我创建了一个在物理设备上运行良好的 flutter 应用程序 phone 但是当我在模拟器上试用它时,不幸的是应用程序停止了。产生错误 java.lang.RuntimeException: Unable to start activity ComponentInfo{big.xxxx.xxxxx/xxx.xxxxx.xxxxx.MainActivity}: android.content.res.Resources$NotFoundException: File res/drawable/launch_background.xml from drawable resource ID #0x7f040019
我已经检查过 launch_background.xml 存在。内容在这里
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="?android:colorBackground" />
<!-- You can insert your own image assets here -->
<!-- <item>
<bitmap
android:gravity="center"
android:src="@mipmap/launch_image" />
</item> -->
</layer-list>
看起来您已经删除了 launch_background.xml 这是您使用 Flutter 时在 Android 中执行的第一个屏幕。如果您有 launch_background.xml,请检查路径 /android/app/src/main/drawable。
如果你不这样做,你必须添加到你的项目中。
在四处询问后,我能够通过删除调用可绘制文件的实例来解决问题。
这主要分别在我的 styles.xml 文件和 Manifest.xml 文件中。
<item name="android:windowBackground">@drawable/launch_background</item>
<meta-data android:name="io.flutter.embedding.android.SplashScreenDrawable" android:resource="@drawable/launch_background" />
有机会问另一个以不同方式解决这个问题的人,这就是他不得不说的。
根本原因
根本原因似乎是期望位图但没有得到位图。即使@mipmap/ic_launcher 指向一些 PNG(参见 res/mipmap-hdpi),在 API >= 26 上,它会指向这个 XML 而 Android 没有好像很喜欢
所以在我的例子中,引用@drawable(它总是一个 PNG == 一个漂亮的位图)而不是 @mipmap(它可能是一个自适应图标 == 一个奇怪的位图?如果一个位图?)修复它。
换句话说:不要将 an 用作 .
的 android:src
如果我今天要这样做,我会尝试矢量可绘制对象(只需在 Android Studio 中导入 SVG 以获得矢量 XML)是否也能正常工作(而不是必须使用 PNG)。
希望有所帮助(或至少解释了我的这一变化)。
请仔细检查您是否遇到了同样的问题 - Resources$NotFoundException 可能会因许多不同的原因而抛出,例如资源位于 drawable-v26 文件夹中,因此在 运行 时被忽略,例如API 24. 有时 gradle 构建日志会显示一些警告,指出 XML 编译失败(但构建仍然成功,然后在运行时崩溃)。
如果不是同一个问题,最好在互联网上搜索 Resources$NotFoundException 的其他原因和修复。
只需在android\app\src\main\AndroidManifest.xml
中注释掉这个
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"/>
我创建了一个在物理设备上运行良好的 flutter 应用程序 phone 但是当我在模拟器上试用它时,不幸的是应用程序停止了。产生错误 java.lang.RuntimeException: Unable to start activity ComponentInfo{big.xxxx.xxxxx/xxx.xxxxx.xxxxx.MainActivity}: android.content.res.Resources$NotFoundException: File res/drawable/launch_background.xml from drawable resource ID #0x7f040019
我已经检查过 launch_background.xml 存在。内容在这里
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="?android:colorBackground" />
<!-- You can insert your own image assets here -->
<!-- <item>
<bitmap
android:gravity="center"
android:src="@mipmap/launch_image" />
</item> -->
</layer-list>
看起来您已经删除了 launch_background.xml 这是您使用 Flutter 时在 Android 中执行的第一个屏幕。如果您有 launch_background.xml,请检查路径 /android/app/src/main/drawable。 如果你不这样做,你必须添加到你的项目中。
在四处询问后,我能够通过删除调用可绘制文件的实例来解决问题。 这主要分别在我的 styles.xml 文件和 Manifest.xml 文件中。
<item name="android:windowBackground">@drawable/launch_background</item>
<meta-data android:name="io.flutter.embedding.android.SplashScreenDrawable" android:resource="@drawable/launch_background" />
有机会问另一个以不同方式解决这个问题的人,这就是他不得不说的。
根本原因
根本原因似乎是期望位图但没有得到位图。即使@mipmap/ic_launcher 指向一些 PNG(参见 res/mipmap-hdpi),在 API >= 26 上,它会指向这个 XML 而 Android 没有好像很喜欢
所以在我的例子中,引用@drawable(它总是一个 PNG == 一个漂亮的位图)而不是 @mipmap(它可能是一个自适应图标 == 一个奇怪的位图?如果一个位图?)修复它。
换句话说:不要将 an 用作 .
的 android:src如果我今天要这样做,我会尝试矢量可绘制对象(只需在 Android Studio 中导入 SVG 以获得矢量 XML)是否也能正常工作(而不是必须使用 PNG)。
希望有所帮助(或至少解释了我的这一变化)。
请仔细检查您是否遇到了同样的问题 - Resources$NotFoundException 可能会因许多不同的原因而抛出,例如资源位于 drawable-v26 文件夹中,因此在 运行 时被忽略,例如API 24. 有时 gradle 构建日志会显示一些警告,指出 XML 编译失败(但构建仍然成功,然后在运行时崩溃)。
如果不是同一个问题,最好在互联网上搜索 Resources$NotFoundException 的其他原因和修复。
只需在android\app\src\main\AndroidManifest.xml
中注释掉这个<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"/>