【问题标题】:Dagger Hilt. Runtime error. Error: ViewModel has no zero argument constructor (kotlin)匕首柄。运行时错误。错误:ViewModel 没有零参数构造函数 (kotlin)
【发布时间】:2021-06-09 16:47:14
【问题描述】:

我收到一个错误(MainViewModel 没有零参数构造函数)。 在我看来,错误在于滥用 Hilt,但我找不到。 SA上有类似的问题,但它们不适合我的情况。 我找不到哪里出错了,如果有任何帮助,我将不胜感激。

错误:

java.lang.RuntimeException: Cannot create an instance of class mypackage.main.MainViewModel
  /* bla bla bla */
Caused by: java.lang.InstantiationException: java.lang.Class<mypackage.main.MainViewModel> has no zero argument constructor
 at java.lang.Class.newInstance(Native Method)
 at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
 ... 39 more

ViewModel 是这样开始的:

@HiltViewModel
class MainViewModel @Inject constructor(
    private val repo: MainRepository,
    private val dispatchers: DispatcherProvider
) : ViewModel() {
 
    // body
}

在 MainActivity 中:

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    private val viewModel: MainViewModel by viewModels()
// etc

应用模块:

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

    @Singleton
    @Provides
    fun provideCurrencyApi() : CurrencyApi = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
        .create(CurrencyApi::class.java)

    @Singleton
    @Provides
    fun provideMainRepository(api: CurrencyApi): MainRepository = DefaultMainRepository(api)

    @Singleton
    @Provides
    fun provideDispatchers(): // blablabla
    }
}

主存储库:

interface MainRepository {
    suspend fun getRates(base: String) : Resource<CurrencyResponse>
    }

DefaultMainRepository

class DefaultMainRepository @Inject constructor(
    private val api: CurrencyApi
) : MainRepository {

    override suspend fun getRates(base: String): Resource<CurrencyResponse> {
        return try {
            val response = api.getRates(base)
            val result = response.body()
            if (response.isSuccessful && result != null) {
                Resource.Success(result)
            } else {
                Resource.Error(response.message())
            }
        } catch (e: Exception) {
            Resource.Error(e.message ?: "An error occurred")
        }
    }
}

【问题讨论】:

  • 从您的MainViewModelDefaultMainRepository 中删除@Inject constructor。你不需要这个,因为你已经从 dagger 模块提供了这些
  • 感谢您的建议,但它会导致同样的错误
  • 请在删除@Inject constructor时使用匕首模块提供MainViewModel
  • 不能直接作为构造函数传递接口,必须绑定它

标签: android kotlin android-viewmodel dagger-hilt


【解决方案1】:

我通过将 Dagger Hilt 依赖项版本更改为更早版本解决了这个问题。我认为这些版本不匹配。其余的代码结果是正确的,看来..

【讨论】:

  • 您最终使用的是哪个版本?我将 2.28.3-alpha 用于 dagger-hilt,将 1.0.0-alpha03 用于 andreoidx.hilt:hilt-lifecycle-viewmodel 和 hilt-compiler,它的失败方式与您的相同...
  • 添加到我的最后一条评论,我升级到 2.3.7 并删除了 androidx.hilt:hilt-lifecycle-viewmodel 和 hilt-compiler 的依赖项,因为它们不再需要。非常感谢。
  • 现在我使用较旧的: implementation "com.google.dagger:hilt-android:2.28-alpha" kapt "com.google.dagger:hilt-android-compiler:2.28-alpha" implementation "androidx .hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02" kapt "androidx.hilt:hilt-compiler:1.0.0-alpha02" 但我认为当我使用最新版本时,我只是对它们做了一些不正确的事情,也许在那里它们有些不匹配。
猜你喜欢
  • 1970-01-01
  • 2020-09-26
  • 1970-01-01
  • 2018-12-06
  • 1970-01-01
  • 1970-01-01
  • 2017-10-26
  • 1970-01-01
  • 2021-03-11
相关资源
最近更新 更多