无法在约束中表示 214748364 的大小

Ric*_*per 10 android android-jetpack-compose android-jetpack-compose-layout

我刚刚创建了一个简单的 Composable 并希望使用 a 来渲染它Layout,但是在实现解决方案时,我在测量阶段偶然发现了这个错误。

java.lang.IllegalArgumentException: Can't represent a size of 214748364 in Constraints
        at androidx.compose.ui.unit.Constraints$Companion.bitsNeedForSize(Constraints.kt:408)
        at androidx.compose.ui.unit.Constraints$Companion.createConstraints-Zbe2FdA$ui_unit_release(Constraints.kt:368)
        at androidx.compose.ui.unit.ConstraintsKt.Constraints(Constraints.kt:438)
        at androidx.compose.ui.unit.ConstraintsKt.Constraints$default(Constraints.kt:423)
        at com.gmarsk.aiare.MainActivity$InstructionsScreen$DisplayCard$1.measure-3p2s80s(MainActivity.kt:514)
        at androidx.compose.ui.node.InnerPlaceable.measure-BRTryo0(InnerPlaceable.kt:54)
Run Code Online (Sandbox Code Playgroud)

这是我尝试这个的时候

val sampleComposable = measurables[1].measure(
                Constraints(
                    minHeight = constraints.maxHeight * 7 / 10,
                )
            )
Run Code Online (Sandbox Code Playgroud)

有人以前偶然发现过这个吗?如果不是错误,有什么解决方案,请告诉我。

现在,我认为这里的问题是我嵌套了两个Layout可组合项,

Layout(
content = {
 Dummy1()
 Dummy2()
 NestedLayoutComposable() // It contains a Layout Composable itself
}{ measurables, constraints ->
 val nlc = measurables[2].measure(
  Constraints(
   minHeight = constraints.maxHeight * 7/10
  )
 )
 layout(constraints.maxWidth, constraints.maxHeight){
  nls.place(0, 0)
 }
}
)
Run Code Online (Sandbox Code Playgroud)

嵌套的LayoutComposable再次有一个布局,这就是崩溃发生的地方,就在这一行

            Layout(
                content = {
                    Text(text = "Random")
                    Box {
                        Image(painter = AppUtils.getRandomImagePainter(), contentDescription = "")
                    }
                }
            ) { measurables, constraints ->
                val text = measurables[0].measure(constraints)
/*This line -->*/   val image = measurables[1].measure(
                    Constraints(
                        maxWidth = constraints.maxWidth * 90 / 100,
                        minHeight = constraints.maxHeight * 7 / 10
                    )
                )

                layout(constraints.maxWidth, constraints.maxHeight) {
                    instruction.place(
                        (constraints.maxWidth - instruction.width) / 2,
                        0
                    )
                    illustration.place(
                        (constraints.maxWidth - illustration.width) / 2,
                        illustration.height / 2
                    )
                }
            }

Run Code Online (Sandbox Code Playgroud)

所以我知道问题在于Layout可组合项相互嵌套,但这仍然不能解释为什么会发生错误,以及如何解决它,所以这些是这篇文章的主要查询,这就是我所期望的包括答案,谢谢。

Col*_*ght 4

在我的例子中,类似的崩溃是由另一个可滚动列内的可滚动列引起的。检查您的撰写层次结构中是否没有这种情况。