Hilt: kotlin.UninitializedPropertyAccessException: свойство lateinit не было инициализировано ⇐ Android

Форум для тех, кто программирует под Android
Anonymous
Hilt: kotlin.UninitializedPropertyAccessException: свойство lateinit не было инициализировано

Сообщение Anonymous »

Я использовал Hilt, но возникла ошибка: kotlin.UninitializedPropertyAccessException: свойство lateinit cacheInterceptor не было инициализировано. Как это исправить?
@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
}

}


Подробнее здесь: https://stackoverflow.com/questions/785 ... as-not-bee

Вернуться в «Android»