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

Viv*_*art 10 android android-jetpack-compose dagger-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
}
Run Code Online (Sandbox Code Playgroud)

这些是我的依赖项

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'
Run Code Online (Sandbox Code Playgroud)

ksk*_*d19 7

根据您的依赖项,我怀疑您正在使用 NavController/NavHost。

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

现在在定义 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)
    }
  }
}

...
Run Code Online (Sandbox Code Playgroud)

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

  • 我根据这个答案从项目中删除了`androidx.navigation:navigation-compose`,我不敢相信这一直是问题所在‍♂️ (2认同)

Dươ*_*inh 7

hiltNavGraphViewModel()扩展已被弃用。hiltViewModel()如果您使用 Navigation 和 Hilt,则可以改为使用。

首先,添加此依赖项:

implementation 'androidx.hilt:hilt-navigation-compose:1.0.0-alpha03'
Run Code Online (Sandbox Code Playgroud)

现在您可以hiltViewModel()与导航一起使用。

@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)
        }
        /* ... */
    }
}
Run Code Online (Sandbox Code Playgroud)

更多信息:刀柄和导航