简体   繁体   中英

Getting "Can't find ColorStateList from drawable resource ID" when adding AlertDialog in Jetpack Compose

I'm getting this Can't find ColorStateList from drawable resource ID #0x108028b error when adding AlertDialog in my app.

I also tried following this answer , but it didn't help. if android:statusBarColor is the issue then this is the code I'm using in my 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>

Compose code:

@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")
                        }
                    }
                )
            }
        }
    }
}

I'm not sure what exactly is causing this issue, need some help to resolve this. Thanks.

Solved it.

The problem was in below line, not sure why ?android:attr/windowBackground didn't work but changing to simple @color/yourColor solved.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM