android - 在 ViewModel 中使用 hilt 注入(inject)时,无法在可组合函数中使用 ViewModel

标签 android kotlin dependency-injection android-jetpack-compose dagger-hilt

我尝试在可组合函数中使用我的 hilt viewModel,但我不断收到错误:

“java.lang.RuntimeException:无法创建类 com.example.tryingtogettheviewmodeltowork.MainViewModel 的实例”

我在我的 viewModel 中使用简单的柄注入(inject)来复制我在实际正在处理的应用程序上遇到的错误。 Android 文档说,在可组合函数中不需要执行任何其他操作即可使用 hilt viewModel ( https://developer.android.com/jetpack/compose/libraries#hilt ),但是每次尝试其他解决方案时,我都会遇到相同的错误。

我的viewModel类:

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
    }

}

我的具有可组合函数的 MainActivity 类:

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

    }
}

最佳答案

我发现了我的错误。您必须将 @AndroidEntryPoint 添加到 MainActivity,而不是尝试将其添加到可组合函数之上。

关于android - 在 ViewModel 中使用 hilt 注入(inject)时,无法在可组合函数中使用 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69297900/

相关文章:

java - Android 音频标准化或调平

android 从 WebView 关闭当前 Activity

android - 在Android中使用Room时如何检查数据库中是否有某个项目?

inheritance - Kotlin 中的 var 可以用带有继承类的 val 覆盖吗?

android - 如何使用 Kodein 为工厂类实例化许多参数

c# - 创建 CaSTLe.Windsor 拦截器的问题

android - 关于ionic框架和从服务器获取数据

kotlin - Kotlin 如何调度调用运算符?

c# - 如何在 ASP.NET 5 (vNext) 中设置 SignalR Hub 依赖注入(inject)?

android - 单击抽屉导航项目,抽屉导航中的新 ListView