I am having trouble with Dagger Hilt in my Android application. Although I have implemented the required components, the app still throws an IllegalStateException. Below are the details of my configuration:
Viewmodel:
@HiltViewModel
class TodoListViewModel @Inject constructor(
private val repository: ToDoRepository
): ViewModel() {
val todos = repository.getToDos()
private val _uiEvent = Channel<UIEvent>()
val uiEvent = _uiEvent.receiveAsFlow()
private var deletedTodo: ToDo? = null
fun onEvent(event: ToDoListEvent) {
when(event) {
is ToDoListEvent.OnToDoClick -> {
sendUiEvent(UIEvent.Navigate(Routes.ADD_EDIT_TODO + "?todoId=${event.toDo.id}"))
}
is ToDoListEvent.OnAddToDoClick -> {
sendUiEvent(UIEvent.Navigate(Routes.ADD_EDIT_TODO))
}
is ToDoListEvent.OnUndoDeleteClick -> {
deletedTodo?.let { todo ->
viewModelScope.launch {
repository.insertToDo(todo)
}
}
}
is ToDoListEvent.OnDeleteToDoClick -> {
viewModelScope.launch {
deletedTodo = event.toDo
repository.deleteToDo(event.toDo)
sendUiEvent(UIEvent.ShowSnackBar(
message = "Todo deleted",
action = "Undo"
))
}
}
is ToDoListEvent.OnDoneChange -> {
viewModelScope.launch {
repository.insertToDo(
event.toDo.copy(
isDone = event.isDone
)
)
}
}
}
}
private fun sendUiEvent(event: UIEvent) {
viewModelScope.launch {
_uiEvent.send(event)
}
}
}
@HiltAndroidApp
class ToDoApp : Application()
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
ToDoListAppTheme {
ToDoListScreen()
}
}
}
}
Despite these implementations, I am encountering the following error:
FATAL EXCEPTION: main (Ask Gemini)
Process: me.huynhducphu.todolistapp, PID: 19683
java.lang.IllegalStateException: Given component holder class me.huynhducphu.todolistapp.MainActivity does not implement interface dagger.hilt.internal.GeneratedComponent or interface dagger.hilt.internal.GeneratedComponentManager
at dagger.hilt.EntryPoints.get(EntryPoints.java:62)
at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.createInternal(HiltViewModelFactory.java:206)
at androidx.hilt.navigation.HiltViewModelFactory.create(HiltNavBackStackEntry.kt:75)
at androidx.hilt.navigation.compose.HiltViewModelKt.createHiltViewModelFactory(HiltViewModel.kt:95)
at me.huynhducphu.todolistapp.ui.todo_list.ToDoListScreenKt.ToDoListScreen(ToDoListScreen.kt:88)
...
I have checked my Gradle dependencies and ensured that the necessary plugins and annotations are applied. What could be causing this issue? Any help would be appreciated.