【问题标题】:Getting "Can't find ColorStateList from drawable resource ID" when adding AlertDialog in Jetpack Compose在 Jetpack Compose 中添加 AlertDialog 时出现“无法从可绘制资源 ID 中找到 ColorStateList”
【发布时间】:2021-10-23 04:35:09
【问题描述】:

在我的应用中添加 AlertDialog 时,我收到了这个 Can't find ColorStateList from drawable resource ID #0x108028b 错误。

我也尝试关注this answer,但没有帮助。如果android:statusBarColor 是问题所在,那么这就是我在night\themes.xml 中使用的代码

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.AppName" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_200</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/black</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_200</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
<!--        <item name="statusBarBackground">@color/statusBarDark</item>-->
        <item name="android:statusBarColor">?android:attr/colorBackground</item>
        <!-- Navigation bar color. -->
        <item name="android:navigationBarColor">?android:attr/windowBackground</item>
        <!-- Customize your theme here. -->
    </style>

    <style name="ThemeOverlay.AppName.FullscreenContainer" parent="">
        <item name="fullscreenBackgroundColor">@color/light_blue_900</item>
        <item name="fullscreenTextColor">@color/light_blue_A400</item>
    </style>
</resources>

编写代码:

@Composable
fun AlertDialogSample() {
    MaterialTheme {
        Column {
            val openDialog = remember { mutableStateOf(false)  }

            Button(onClick = {
                openDialog.value = true
            }) {
                Text("Click me")
            }

            if (openDialog.value) {
                AlertDialog(
                    onDismissRequest = {
                        openDialog.value = false
                    },
                    title = {
                        Text(text = "Dialog Title")
                    },
                    text = {
                        Text("Here is a text ")
                    },
                    confirmButton = {
                        Button(
                            onClick = {
                                openDialog.value = false
                            }) {
                            Text("This is the Confirm Button")
                        }
                    },
                    dismissButton = {
                        Button(
                            onClick = {
                                openDialog.value = false
                            }) {
                            Text("This is the dismiss Button")
                        }
                    }
                )
            }
        }
    }
}

我不确定到底是什么导致了这个问题,需要一些帮助来解决这个问题。谢谢。

【问题讨论】:

  • 不清楚什么时候出错。
  • @GabrieleMariotti 只要我打开要显示对话框的活动
  • 请添加导致错误的可组合代码
  • @PhilipDukhov 我已经在帖子中附上了上面的撰写代码
  • 我能够用您的代码重现它,但是您在问题中谈到的answer 的解决方案对我有用。也许您在测试轻型主题时仅将其应用于夜间主题?

标签: android kotlin android-alertdialog android-jetpack-compose


【解决方案1】:

解决了。

问题出在下面一行,不知道为什么?android:attr/windowBackground 不起作用,但改成简单的@color/yourColor 解决了。

<item name="android:navigationBarColor">?android:attr/windowBackground</item>

【讨论】:

  • 这正是this answer 所说的,不是吗?
  • 是的,我只是看错了哈哈
  • 我明白了。我建议您投票支持该答案并删除您的问题,因为事实证明这是重复的
猜你喜欢
  • 2016-12-06
  • 2019-02-07
  • 1970-01-01
  • 2020-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多