Reputation: 53
I am trying to use my hilt viewModel in a composable function but I keep getting the error:
"java.lang.RuntimeException: Cannot create an instance of class com.example.tryingtogettheviewmodeltowork.MainViewModel"
I am using simple hilt injection in my viewModel to replicate the error that I am getting on the app I am actually working on. Android documentation says that nothing else needs to be done in the composable function to use a hilt viewModel (https://developer.android.com/jetpack/compose/libraries#hilt)however I keep getting the same error every time I try another solution.
My viewModel class:
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
@HiltViewModel
class MainViewModel @Inject constructor(
val someInt: Int
): ViewModel(){
init{
println(someInt)
}
// Mutable Live data for storing the value of the username entry field
private var _username = MutableLiveData("")
val username: LiveData<String> =_username
// Function for changing the value of the username entry field
fun onUsernameChange(it: String){
_username.value = it
}
}
My MainActivity Class with composable function:
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.tryingtogettheviewmodeltowork.ui.theme.TryingToGetTheViewModelToWorkTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
TryingToGetTheViewModelToWorkTheme {
// A surface container using the 'background' color from the theme
Surface(color = MaterialTheme.colors.background) {
MainScreen()
}
}
}
}
}
@Composable
fun MainScreen(viewModel: MainViewModel = androidx.lifecycle.viewmodel.compose.viewModel()) {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
val userName: String by viewModel.username.observeAsState("")
OutlinedTextField(
value = userName,
onValueChange ={
viewModel.onUsernameChange(it)
},
label = {
Text(text = "User Name")
}
)
}
}
Upvotes: 2
Views: 754
Reputation: 53
I figured out my mistake. You have to add @AndroidEntryPoint to MainActiivty rather than trying to add it on top of a composable function.
Upvotes: 3