【问题标题】:jetpack compose viewModel() is giving error "has no zero argument constructor" with hiltjetpack compose viewModel() 给出错误“没有零参数构造函数”,带有刀柄
【发布时间】:2021-02-26 17:56:22
【问题描述】:

在我的项目中,当我使用刀柄时,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
}

这些是我的依赖项

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'

【问题讨论】:

  • 我也发现了这个。但是在活动中使用by viewModels() 效果很好。猜测这是其中一个 alpha 库中的错误。
  • 您是否在 NavHost 中使用此组合?我也在运行这个问题,并且 Hilt 集成页面已经过时,推荐 HiltViewModelFactory

标签: android android-jetpack-compose dagger-hilt


【解决方案1】:

根据您的依赖关系,我怀疑您使用的是 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)
    }
  }
}

...

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

【讨论】:

  • 我根据这个答案从项目中删除了androidx.navigation:navigation-compose,我不敢相信这一直是问题??‍♂️
【解决方案2】:

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

首先,添加这个依赖:

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

现在您可以将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)
        }
        /* ... */
    }
}

更多信息:Hilt and Navigation

【讨论】:

  • 您知道如何将视图模型限定为嵌套图吗?
【解决方案3】:

当我将 ViewModel 设置在一个名为 new 的包中时,我遇到了这种情况。

假设我的基础包是 com.example.appname,我创建了一个名为 new 的新包,我有 com.example.appname.new,除包 new 中的一个以外,其他所有 ViewModel 工厂都是由 Hilt 成功创建的强>.

  1. 将包重命名为new,看起来是保留的。
  2. 将您的 ViewModel 移动到另一个位置。

【讨论】:

    【解决方案4】:

    确保包含可组合项的 Activity/Fragments 具有 @AndroidEntryPoint 并且您具有最新版本的 hilt 依赖项。

    【讨论】:

    • 这是一个有效的响应,只要您不使用撰写导航并且只想要一个简单的活动范围的视图模型注入(如 OP 所要求的那样)。
    猜你喜欢
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多