致命异常:android.content.res.Resources$NotFoundException 资源 ID #0x7f07008d

Fatal Exception: android.content.res.Resources$NotFoundException Resource ID #0x7f07008d

我在 Crashlytics 中发生了 5 次崩溃。 Resources.java 中的 2x 和 ResourcesImpl 中的 3x:

鉴于 Resources.java 是一个 Android SDK 文件,我看不出代码哪里出错了。我确实有几个地方正在调用资源:

someText = getContext().getResources().getString(R.string.debug_loading);
bitmap = BitmapFactory.decodeResource(applicationContext.resources, R.mipmap.ic_launcher)

我在代码下面的所有地方都添加了它:

    Bitmap bitmap = null;
    try {
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
    } catch (Resources.NotFoundException e) {
        e.printStackTrace();
    }

这是正确的方法吗,因为我们无法对资源 ID 进行逆向工程,因此不知道问题究竟发生在哪里?

你能确认这是否只属于 Android OS 5 和 5.1,如果是那么它似乎是 androidx.appcompat 1.1.0 的错误 参考:- https://issuetracker.google.com/issues/141132133

尝试降级到 androidx.appcompat:appcompat:1.0.2 一次,如果问题得到解决

如果您没有使用应用程序包 (.aab),请忽略所有这些。

如果您,这是由于用户旁加载您的应用程序造成的。例如,如果低端设备上的用户下载了用于高端设备的 APK,他们将无法访问正确的资源。崩溃!

这可以通过 manually checking the installer package 在第一次 运行 / 启动时修复。这不是 100% 可靠。

注意:有previously an excellent library by Google to check all necessary parts of the app exist, and display an install prompt if not. This seems to have disappeared entirely besides basic documentation,会继续努力找出它发生了什么。

我不能告诉你为什么会这样,但我可以帮你找出答案。

您的错误来自 Material 组件库。由于某些原因,test_custom_background 可绘制对象无法位于这些设备中。检查您是否直接使用它,并提供替代方案。如果不是,导致此类错误的可能原因是:

-SDK级别冲突:检查受影响设备的SDK级别。 Google 倾向于搞砸 lower/newer SDK 的主题。

-从设备供应商处删除 SDK 级资源:有时供应商(你好,三星!)为了方便而修改 SDK,在此过程中,他们 delete/change 低级 stuff/resources 你需要。

-旁加载应用程序包。

为了将来参考,ResourcesNotFoundException 资源 ID 是一个字符串,由“0x”加上 ID 转换为基数 16 的无符号十六进制数组成:

void getValue(@AnyRes int id, TypedValue outValue, boolean resolveRefs)
            throws NotFoundException {
        boolean found = mAssets.getResourceValue(id, 0, outValue, resolveRefs);
        if (found) {
            return;
        }
        throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id));
    }

所以,遇到这种错误的时候,反推代码,找ID:

Log.d("Whosebug", "id is "+Integer.parseUnsignedInt("7f07008d", 16))

这给了我们 2131165325,如果在 android 开发者网站上搜索,我们会得到:

test_custom_background
int test_custom_background
Constant Value: 2131165325 (0x7f07008d)

来自 com.google.android.material.R.drawable : https://developer.android.com/reference/com/google/android/material/R.drawable

我希望这可以帮助您找到根本原因。狩猎愉快,节日快乐!