You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetpack Compose中使用Float资源遇NotFound错误求助

解决:Resources$NotFoundException 资源类型无效

原因

你使用dimensionResource()读取纯浮点类型资源,但该API仅用于读取带尺寸单位(dp/sp/px等)的标准dimen资源,类型不匹配导致系统无法解析资源。

你的float.xml中定义的是format="float"的数值,即使标记type="dimen",本质也不是标准尺寸资源,API21对资源类型校验更严格,因此触发异常。

解决方案

方案1:用floatResource()读取浮点资源

替换dimensionResource()为Compose专门读取浮点资源的floatResource():

@Composable
fun LoadingCircle() {
    val currentRotation by transition.animateValue(
        0F,
        targetValue = floatResource(id = R.dimen.loading_circle_target),
        // 剩余代码
    )
    // 剩余代码
}

你的资源定义可以保持不变,建议把数值写成360.0明确浮点类型:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="loading_circle_target" format="float" type="dimen">360.0</item>
</resources>

方案2:改为标准尺寸资源(仅当数值为尺寸时适用)

如果该值是尺寸相关,可添加单位后继续用dimensionResource():

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="loading_circle_target">360.0dp</dimen>
</resources>

但你的场景是旋转角度,方案1更贴合需求。

兼容说明

floatResource()要求Compose UI版本≥1.2.0,确保依赖版本满足;API21对资源类型解析逻辑严格,必须保证资源类型与读取API匹配。

内容的提问来源于stack exchange,提问作者Compose Learner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 16:05:32