创建 Hilt viewModel 时出错 [英] Getting error from creating a Hilt viewModel

查看:68
本文介绍了创建 Hilt viewModel 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个简单的 viewModel 交给一个可组合的,并且每次都得到这个错误,但我不知道这是什么意思:

I've tried to hand over a simple viewModel to a composable and get everytime this error and i don't know what it means:

java.lang.IllegalStateException:给定的组件持有者类 com.example.app.MainActivity 没有实现接口 dagger.hilt.internal.GeneratedComponent 或接口 dagger.hilt.internal.GeneratedComponentManager

我的摇篮:

    implementation("androidx.hilt:hilt-navigation:1.0.0-beta01")
    implementation("androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03")

我已经创建了一个 BaseApplication 文件

I've created a BaseApplication file

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class BaseApplication: Application()

并将其设置在清单中.

我创建了一个 AppModule 文件:

I've created an AppModule file:

import android.content.Context
import com.veloce.montageservice.BaseApplication
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    @Singleton
    @Provides
    fun provideApplication(@ApplicationContext app: Context): BaseApplication {
        return app as BaseApplication
    }
}

然后在我的 MainActivity 中设置 @HiltAndroidApp 并创建一个 viewModel 像这样:

Then set the @HiltAndroidApp in my MainActivity and created a viewModel like this:

@HiltViewModel
class TaskViewModel: ViewModel() {

//code

}

MainActivity 的导航中调用 viewModel:

The viewModel is called in my navigation at the MainActivity:

 val navController = rememberNavController()
 NavHost(
     navController = navController,
     startDestination = Screens.LoginScreen.route
 ) {
 composable(Screens.ActiveMontageScreen.route) { navBackStackEntry ->
     activeTask?.let {
         val factory =
             HiltViewModelFactory(LocalContext.current, navBackStackEntry)
         val viewModel: TaskViewModel = viewModel("taskViewModel", factory)
         ActiveMontageScreen(
             viewModel = viewModel,
             task = it,
             navigation = navController,
             context = LocalContext.current
         ) {
             removeActiveTask()
         }
     }
 }
}

我真的看不出任何问题,但我对 Hilt 和 Android 开发非常缺乏经验.有谁知道是什么问题?

I really cant't see any problems, but I am quite unexperienced with Hilt and Android Development. Does anyone know whats the problem?

推荐答案

据我所知,几乎没有什么地方放错了地方,导致了这样的错误.

There are few things misplaced as I can see, which lead to such error.

例如:

  1. 您没有在此处注入上下文 class TaskViewModel(context: Context) 如果您需要 viewmodel 中的上下文,那么您可以使用 AndroidViewModel.

  1. You're not injecting the context here class TaskViewModel(context: Context) if you need context in viewmodel then you can use AndroidViewModel.

您正在 AppModule 中创建 BaseApplication,但您没有使用它(可能在其他地方使用过).

you're creating BaseApplication in AppModule but you're not using it(Maybe used somewhere else).

您可以通过以下步骤创建这样的视图模型:

You can create such viewmodel with following steps:

  1. 创建BaseApplication &在清单中注册,您已经完成了.

  1. Create BaseApplication & register that in manifest, which you have already done.

创建一个 AndroidViewModel 如下:

@HiltViewModel
class TaskViewModel @Inject constructor(
 application: BaseApplication
) : AndroidViewModel(application) {

 fun test() {
     getApplication<BaseApplication>().getString(R.string.app_name)
 }

}  

这里注意使用 @Inject 构造函数,它将从 AppModule 中获取 BaseApplication 类.

Here notice the use of @Inject constructor which will take the BaseApplication class from AppModule.

  1. 在你的 MainActivity 中像这样初始化 viewmodel:

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

 private val viewModel: TaskViewModel by viewModels()

 override fun onCreate(savedInstanceState: Bundle?) {
     super.onCreate(savedInstanceState)
     setContentView(R.layout.activity_main)

     viewModel.test()
 }

}

如果您无法解决任何与刀柄相关的问题,请确保您在 build.gradle

If you fail to resolve any of the hilt related things then make sure you have these dependencies in build.gradle

//Dagger - Hilt
    implementation "com.google.dagger:hilt-android:2.33-beta"
    kapt "com.google.dagger:hilt-android-compiler:2.33-beta"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
    kapt "androidx.hilt:hilt-compiler:1.0.0-beta01"

这篇关于创建 Hilt viewModel 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆