我最近在 Android Studio Logcat 中发现了一个问题,当我从 Android Studio 运行我的应用程序到我的设备时,或者当我关闭设备上已安装的应用程序后重新启动它时。请问这个问题如何解决?
Logcat 中的消息下方:
java.util.concurrent.ExecutionException: android.content.res.Resources$NotFoundException: Drawable com.compagny_name.app_name:mipmap/ic_launcher with resource ID #0x7f0f0000
at java.util.concurrent.FutureTask.report(FutureTask.java:123)
at java.util.concurrent.FutureTask.get(FutureTask.java:207)
at android.content.res.Resources.fetchDrawableFromFutureCache(Resources.java:2751)
at android.content.res.Resources.loadDrawable(Resources.java:1064)
at android.content.res.Resources.getDrawableForDensity(Resources.java:1048)
at android.content.res.Resources.getDrawable(Resources.java:987)
at android.content.Context.getDrawable(Context.java:758)
at com.android.internal.widget.ToolbarWidgetWrapper.setIcon(ToolbarWidgetWrapper.java:322)
at com.android.internal.widget.ActionBarOverlayLayout.setIcon(ActionBarOverlayLayout.java:755)
at com.android.internal.policy.PhoneWindow.setDefaultIcon(PhoneWindow.java:1884)
at android.app.Activity.initWindowDecorActionBar(Activity.java:3575)
at android.app.Activity.getActionBar(Activity.java:3508)
at com.compagny_name.app_name.ui.container.MainActivity.onCreate(MainActivity.kt:433)
at android.app.Activity.performCreate(Activity.java:8290)
at android.app.Activity.performCreate(Activity.java:8270)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4085)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4277)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8751)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
Caused by: android.content.res.Resources$NotFoundException: Drawable com.compagny_name.app_name:mipmap/ic_launcher with resource ID #0x7f0f0000
Caused by: android.content.res.Resources$NotFoundException: File res/mipmap-anydpi-v21/ic_launcher.xml from drawable resource ID #0x7f0f0000
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:1039)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:825)
at android.content.res.Resources$submitToFuture.call(Resources.java:2856)
at android.content.res.Resources$submitToFuture.call(Resources.java:2838)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:920)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.app.Application.getResources()' on a null object reference
at android.graphics.drawable.AdaptiveIconDrawable.<init>(AdaptiveIconDrawable.java:165)
at android.graphics.drawable.AdaptiveIconDrawable.<init>(AdaptiveIconDrawable.java:152)
at android.graphics.drawable.DrawableInflater.inflateFromTag(DrawableInflater.java:170)
at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:143)
at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1426)
at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1385)
at android.content.res.ResourcesImpl.loadXmlDrawable(ResourcesImpl.java:1071)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:1010)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:825)
at android.content.res.Resources$submitToFuture.call(Resources.java:2856)
at android.content.res.Resources$submitToFuture.call(Resources.java:2838)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:920)
这是主 Activity 的 onCreate() 方法中的代码:
MainTheme() {
Surface(
modifier = modifier
.fillMaxSize()
.padding(0.dp)
.background(Color.Transparent)
.border(
width = 0.dp,
color = Color.Transparent,
shape = RectangleShape
),
shape = RectangleShape,
color = Color.Transparent,
contentColor = contentColorFor(Color.Transparent),
tonalElevation = 0.dp,
shadowElevation = 0.dp,
border = null,
) {
....
}
}
这是res文件夹的层次结构:
ic_launcher.xml
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
<monochrome android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
使用设备:
配备 Android 12 的三星 Galaxy S10
设置:
Android Studio --> Jellyfish | 2023.3.1 RC
SDK 构建工具 --> 34.0.0
AGP --> 8.4.0-rc02
Kotlin --> 1.9.23
撰写 --> BOM 2024.04.00 |编译器1.5.11
匕首 --> 2.51.1
最小 SDK --> 26
编译SDK --> 34
目标 SDK --> 34
我找到了解决方案!
在 5 个文件夹 mipmap-xxxx(mipmap-mdpi、mipmap-hdpi 等)中,所有“ic_laucher”文件都应具有 PNG 扩展名,而不是我的应用程序中以前使用的 WEBP。