gpt4 book ai didi

android - java.lang.IllegalStateException : Composition requires an active composition context (Android Jetpack Compose)

转载 作者:行者123 更新时间:2023-12-02 13:12:06 24 4
gpt4 key购买 nike

尝试使用 Jetpack Compose 显示 AlertDialog,但应用程序在调用 AlertDialog 函数时崩溃,错误为

java.lang.IllegalStateException: Composition requires an active composition context



请在下面找到代码,
@Composable
fun homeScreenCompose() {

Align(
alignment = Alignment.Center
) {
Column(
arrangement = Arrangement.Center,
modifier = Spacing(16.dp),
children = {

Button(
modifier = Height(50.dp) wraps Expanded,
text = "Alert Dialog", onClick = {
showAlertDialog()
}, style = OutlinedButtonStyle(
Border(color = Color.Red, width = 1.dp),
shape = RoundedCornerShape(50), //50% percent
contentColor = Color.Red,
color = Color.White,
elevation = Dp(4f)
)
)
}
)
}

}

@Composable
fun showAlertDialog() {
val openDialog = +state { true }
if (openDialog.value) {
AlertDialog(
onCloseRequest = {
},
title = {
Text(text = "Logout")
},
text = {
Text("Are you sure you have to logout?")
},
confirmButton = {
Button("Yes", onClick = {
openDialog.value = false
})
},
dismissButton = {
Button("No", onClick = {
openDialog.value = false
})
},
buttonLayout = AlertDialogButtonLayout.Stacked
)
}

}

无法弄清楚它为什么会崩溃以及解决方案是什么,
任何帮助将不胜感激。

谢谢你。

最佳答案

  • 为状态模型创建一个数据类,它不必用@Model 注解标记。
  • 初始状态本身是在 @Composable 函数中使用 + 状态创建的。
  • 对话框的可见性由调用 value 属性获得的模型的 visible 属性决定。
  • 这个属性也可以设置为一个新的不可变对象(immutable对象),就像两个按钮的 onClick 一样。第一个隐藏自己,第二个 - 关闭对话。可以通过单击在同一 @Composable 函数中定义的 Ok 按钮重新打开该对话框。

  • 尝试在此函数之外创建状态时,会发生错误:

    java.lang.IllegalStateException: Composition requires an active composition context.



    可以通过分配 setContent {} 的值来获得上下文。 onCreateView中的函数,但是如何使用它,例如,在 Presenter 或 Fragment 以外的其他类中或 Activity ,对于改变状态仍不清楚。

    查看下面的示例
     data class DialogVisibleModel(val visible: Boolean, val dismissPushed: Boolean = false)
    ...
    @Composable
    fun homeScreenCompose() {

    Column {
    SideBySideAlertDialogSample()
    }

    }

    @Composable
    fun SideBySideAlertDialogSample() {
    val openDialog = +state { DialogVisibleModel(false) }

    Button(text = "Ok", onClick = { openDialog.value = DialogVisibleModel(true) })

    if (openDialog.value.visible) {
    AlertDialog(
    onCloseRequest = {
    // Because we are not setting openDialog.value to false here,
    // the user can close this dialog only via one of the buttons we provide.
    },
    title = {
    Text(text = "Title")
    },
    text = {
    Text("This area typically contains the supportive text" +
    " which presents the details regarding the Dialog's purpose.")
    },
    confirmButton = {
    Button("Confirm", onClick = {
    openDialog.value = DialogVisibleModel(false)
    })
    },
    dismissButton = {
    if (!openDialog.value.dismissPushed)
    Button("Dismiss", onClick = {
    openDialog.value = DialogVisibleModel(true, true)
    })
    else {
    //hidden
    }
    },
    buttonLayout = AlertDialogButtonLayout.SideBySide
    )
    }
    }

    关于android - java.lang.IllegalStateException : Composition requires an active composition context (Android Jetpack Compose),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59853611/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com