我遇到了一个问题,与Hilt和ViewModel。我得到RunTimeException:
MainViewModel> has no zero argument constructor
我花了几个小时来找出问题所在...
以下是我的代码源:
应用模块:
@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(): DispatcherProvider = object : DispatcherProvider {
override val main: CoroutineDispatcher
get() = Dispatchers.Main
override val io: CoroutineDispatcher
get() = Dispatchers.IO
override val default: CoroutineDispatcher
get() = Dispatchers.Default
override val unconfined: CoroutineDispatcher
get() = Dispatchers.Unconfined
}
}
主要活动:
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private val viewModel: MainViewModel by viewModels()
主视图模型:
@HiltViewModel
class MainViewModel @Inject constructor(
private val repository: MainRepository,
private val dispatchers: DispatcherProvider
) : ViewModel() {
MainRepository
class DefaultMainRepository @Inject constructor(
private val api: CurrencyApi
) : MainRepository {
以下是Gradle依赖项:
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
implementation "androidx.activity:activity-ktx:1.4.0"
implementation 'com.google.dagger:hilt-android:2.40.5'
kapt 'com.google.dagger:hilt-compiler:2.40.5'
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
kapt'androidx.hilt:hilt-compiler:1.0.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.0'
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.40.5'
kaptAndroidTest 'com.google.dagger:hilt-compiler:2.40.5'
testImplementation 'com.google.dagger:hilt-android-testing:2.40.5'
kaptTest 'com.google.dagger:hilt-compiler:2.40.5'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
适用范围:
@HiltAndroidApp
class CurrencyApplication: Application()
清单:
<application
android:name=".CurrencyApplication"
我在到处寻找解决方案,就像这里:possible reason
我将感激任何帮助
3条答案
按热度按时间1wnzp6jl1#
首先,检查anotationProcessor以确保hilt能够生成正确的绑定,因此请转到app.gradle并检查是否存在这些依赖项:
i86rm4rw2#
您使用了两次'kapt hilt-compiler'。
我不确定这是一个问题,但我正在使用这个,它的工作原理:
iovurdzv3#
你没有在这里提到,所以我认为你忘了在你的应用程序中添加
@HiltAndroidApp
。然后在你的manifest文件中添加
android:name=".MyOwnApplication"
到你的application标签中。