Hilt: kotlin.UninitializedPropertyAccessException: lateinit-Eigenschaft wurde nicht initialisiert
Posted: 14 Jan 2025, 12:49
Ich verwende Hilt, aber es hat einen Fehler ausgelöst: kotlin.UninitializedPropertyAccessException: lateinit property cacheInterceptor has not been initialized. Wie kann ich das Problem beheben?
Code: Select all
@Module
@InstallIn(SingletonComponent::class)
object NetWorkModule {
@Singleton
@Provides
fun provideCacheInterceptor(): CacheInterceptor {
return CacheInterceptor()
}
}
class CacheInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val response: Response = chain.proceed(chain.request())
val cacheControl = CacheControl.Builder()
.maxAge(1, TimeUnit.DAYS)
.build()
return response.newBuilder()
.header("Cache-Control", cacheControl.toString())
.build()
}
}
@HiltViewModel
class SharedDataViewModel @Inject constructor(
) {
@Inject lateinit var cacheInterceptor: CacheInterceptor
private fun load() {
val client = OkHttpClient().newBuilder()
.addNetworkInterceptor(cacheInterceptor) //kotlin.UninitializedPropertyAccessException: //lateinit property cacheInterceptor has not been initialized
}
}