简体   繁体   English

jetpack compose viewModel() 给出错误“没有零参数构造函数”,带有刀柄

[英]jetpack compose viewModel() is giving error “has no zero argument constructor” with hilt

In my project, compose viewModel() method is giving error "has no zero argument constructor" when I am using hilt.在我的项目中,当我使用刀柄时,compose viewModel() 方法给出错误“没有零参数构造函数”。

@Composable
    fun HomeScreen(homeViewModel: HomeViewModel = viewModel()) {
    ...
    }
    
    @HiltViewModel
    class HomeViewModel @Inject constructor(private val repository: Repository) :
        ViewModel() {
    ...
    }
@Module
@InstallIn(ViewModelComponent::class)
abstract class DataModule {
    @Binds
    abstract fun bindRepository(
        fakePuppyRepository: RepositoryImpl
    ): Repository
}

these are my dependencies这些是我的依赖

implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0'
    implementation 'androidx.activity:activity-compose:1.3.0-alpha03'
    implementation "androidx.navigation:navigation-compose:1.0.0-alpha08"
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.0'
    implementation 'androidx.compose.runtime:runtime-livedata:1.0.0-beta01'
    implementation "com.google.dagger:hilt-android:$hilt_version"
    implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
    kapt "com.google.dagger:hilt-compiler:$hilt_version"
    kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha03'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

Based on your dependencies, I suspect you are using NavController/NavHost.根据您的依赖关系,我怀疑您使用的是 NavController/NavHost。

Add this dependency: androidx.hilt:hilt-navigation-compose:1.0.0-alpha01添加此依赖项: androidx.hilt:hilt-navigation-compose:1.0.0-alpha01

Now in your composable which defines NavController, call HomeScreen as follows:现在在定义 NavController 的可组合物中,按如下方式调用 HomeScreen:

import androidx.compose.runtime.Composable
import androidx.hilt.navigation.compose.hiltNavGraphViewModel
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController

...

@Composable
fun Main() {
  val navController = rememberNavController()
  NavHost(navController, startDestination = "home"){
    composable("home") {
      val model: HomeViewModel = hiltNavGraphViewModel(it)
      HomeScreen(model)
    }
  }
}

...

hiltNavGraphViewModel(...) can also be replaced with viewModel(HiltViewModelFactory(LocalContext.current, backStackEntry)) (from https://github.com/google/dagger/issues/2166#issuecomment-769162910 ) hiltNavGraphViewModel(...)也可以替换为viewModel(HiltViewModelFactory(LocalContext.current, backStackEntry)) (来自https://github.com/google/dagger/issues/2166#issuecomment-769162910

The hiltNavGraphViewModel() extension has been deprecated. hiltNavGraphViewModel()扩展已被弃用。 You can use hiltViewModel() instead if you use Navigation and Hilt.如果您使用 Navigation 和 Hilt,则可以使用hiltViewModel()

First, add this dependency:首先,添加这个依赖:

implementation 'androidx.hilt:hilt-navigation-compose:1.0.0-alpha03'

And now you can use hiltViewModel() together with Navigation.现在您可以将hiltViewModel()与 Navigation 一起使用。

@Composable
fun MyApp() {
    NavHost(navController, startDestination = startRoute) {
        composable("example") { backStackEntry ->
            // Creates a ViewModel from the current BackStackEntry
            // Available in the androidx.hilt:hilt-navigation-compose artifact
            val exampleViewModel = hiltViewModel<ExampleViewModel>()
            ExampleScreen(exampleViewModel)
        }
        /* ... */
    }
}

More information: Hilt and Navigation更多信息: 刀柄和导航

Happened to me when I set my ViewModel inside a package called new .当我将 ViewModel 设置在名为new的 package 中时发生在我身上。

Let's say my base package is com.example.appname , I created a new pakage called new and I had com.example.appname.new , every other ViewModel factories were created successfully by Hilt with the exception the one inside of package new . Let's say my base package is com.example.appname , I created a new pakage called new and I had com.example.appname.new , every other ViewModel factories were created successfully by Hilt with the exception the one inside of package new .

  1. Rename the package with name new , it looks like it is reserved.将 package 重命名为new ,看起来它是保留的。
  2. Move your ViewModel to another location.将您的 ViewModel 移动到另一个位置。

Make sure your Activity/Fragments which enclose the composables have @AndroidEntryPoint and you have the latest version of hilt dependencies.确保包含可组合项的 Activity/Fragments 具有@AndroidEntryPoint并且您具有最新版本的 hilt 依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 ViewModel 的 Jetpack Compose DataStore 错误,“Viewmodel 没有零参数构造函数” - Jetpack Compose DataStore error using ViewModel, “Viewmodel has no zero argument constructor” Hilt ViewModel 没有零参数构造函数 - Hilt ViewModel has no zero argument constructor ViewModel 没有零参数构造函数刀柄 Java - ViewModel has no zero argument constructor hilt Java Hilt Android ViewModel 没有零参数构造函数 - Hilt Android ViewModel has no zero argument constructor Android:注入刀柄的 ViewModel 抛出没有零参数构造函数” - Android: ViewModel injected with hilt throws has no zero argument constructor" 匕首柄。 运行时错误。 错误:ViewModel 没有零参数构造函数 (kotlin) - Dagger Hilt. Runtime error. Error: ViewModel has no zero argument constructor (kotlin) ViewModel 没有零参数构造函数 - ViewModel has no zero argument constructor 在仪器测试零参数构造函数中由 Hilt 注入可组合的 Real ViewModel - Real ViewModel with Composable Injected by Hilt in Instrumentation Test Zero Argument Constructor ViewModel 没有零参数构造函数错误 - 即使它具有零参数构造函数 - ViewModel has no zero argument constructor error - even when it has a zero argument constructor Android + Kotlin + Hilt:活动没有零参数构造函数 - Android + Kotlin + Hilt: Activity has no zero argument constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM