最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Unable to get correct import for AlertDialog (Or deprecated import?) - Stack Overflow

programmeradmin2浏览0评论

I'm following a Room tutorial for Kotlin and (at about 40 minutes) I get the following error:

Below is my code (I checked it and it seems exactly how shown in the video)

@Composable
fun AddContactDialog(
    state: ContactState,
    onEvent: (ContactEvent) -> Unit,
    modifier: Modifier = Modifier
){
    AlertDialog(
        modifier = modifier,
        onDismissRequest = {
            onEvent(ContactEvent.HideDialog)
        },
        title = { Text(text = "Add contact") },
        text = {
            Column(
                verticalArrangement = Arrangement.spacedBy(8.dp)
            ){
                TextField(
                    value = state.firstName,
                    onValueChange = {
                        onEvent(ContactEvent.SetFirstName(it))
                    },
                    placeholder = {
                        Text(text = "First Name")
                    }
                )
                TextField(
                        value = state.lastName,
                    onValueChange = {
                        onEvent(ContactEvent.SetLastName(it))
                    },
                    placeholder = {
                        Text(text = "Last Name")
                    }
                )
                TextField(
                    value = state.phoneNumber,
                    onValueChange = {
                        onEvent(ContactEvent.SetPhoneNumber(it))
                    },
                    placeholder = {
                        Text(text = "Phone Number")
                    }
                )
            }
        },
        buttons = {
            Box(
                modifier = Modifier.fillMaxWidth(),
                contentAlignment = Alignment.CenterEnd
            ){
                Button(onClick = {
                    onEvent(ContactEvent.SaveContact)
                }){
                    Text(text = "Save")
                }
            }
        }
    )
}

Here are my imports:

import androidx.appcompat.app.AlertDialog
import androidxpose.foundation.layout.Arrangement
import androidxpose.foundation.layout.Box
import androidxpose.foundation.layout.Column
import androidxpose.foundation.layout.fillMaxWidth
import androidxpose.material3.Button
import androidxpose.material3.ExperimentalMaterial3Api
import androidxpose.material3.Text
import androidxpose.material3.TextField
import androidxpose.runtime.Composable
import androidxpose.ui.Alignment
import androidxpose.ui.Modifier
import androidx.room.util.TableInfo
import androidxpose.ui.unit.dp

I'm following a Room tutorial for Kotlin and (at about 40 minutes) I get the following error:

Below is my code (I checked it and it seems exactly how shown in the video)

@Composable
fun AddContactDialog(
    state: ContactState,
    onEvent: (ContactEvent) -> Unit,
    modifier: Modifier = Modifier
){
    AlertDialog(
        modifier = modifier,
        onDismissRequest = {
            onEvent(ContactEvent.HideDialog)
        },
        title = { Text(text = "Add contact") },
        text = {
            Column(
                verticalArrangement = Arrangement.spacedBy(8.dp)
            ){
                TextField(
                    value = state.firstName,
                    onValueChange = {
                        onEvent(ContactEvent.SetFirstName(it))
                    },
                    placeholder = {
                        Text(text = "First Name")
                    }
                )
                TextField(
                        value = state.lastName,
                    onValueChange = {
                        onEvent(ContactEvent.SetLastName(it))
                    },
                    placeholder = {
                        Text(text = "Last Name")
                    }
                )
                TextField(
                    value = state.phoneNumber,
                    onValueChange = {
                        onEvent(ContactEvent.SetPhoneNumber(it))
                    },
                    placeholder = {
                        Text(text = "Phone Number")
                    }
                )
            }
        },
        buttons = {
            Box(
                modifier = Modifier.fillMaxWidth(),
                contentAlignment = Alignment.CenterEnd
            ){
                Button(onClick = {
                    onEvent(ContactEvent.SaveContact)
                }){
                    Text(text = "Save")
                }
            }
        }
    )
}

Here are my imports:

import androidx.appcompat.app.AlertDialog
import androidxpose.foundation.layout.Arrangement
import androidxpose.foundation.layout.Box
import androidxpose.foundation.layout.Column
import androidxpose.foundation.layout.fillMaxWidth
import androidxpose.material3.Button
import androidxpose.material3.ExperimentalMaterial3Api
import androidxpose.material3.Text
import androidxpose.material3.TextField
import androidxpose.runtime.Composable
import androidxpose.ui.Alignment
import androidxpose.ui.Modifier
import androidx.room.util.TableInfo
import androidxpose.ui.unit.dp
Share Improve this question edited Nov 20, 2024 at 17:58 genespos asked Nov 20, 2024 at 16:52 genesposgenespos 3,3116 gold badges43 silver badges76 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 5

androidx.appcompat.app.AlertDialog is not the correct import, AlertDialog is a composable and is imported like this:

import androidxpose.material3.AlertDialog

There are now multiple AlertDialog composables available (one is deprecated), but none that matches the parameters you provided. The one you want needs a confirmButton parameter instead of the buttons parameter. Just rename the parameter accordingly and everything should work fine.

发布评论

评论列表(0)

  1. 暂无评论