Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

279
Vistas
LifecycleObserver produce exception with methods that use newer APIs

My ViewModel class implements LifecycleObserver. When I call fragment.lifecycle.addObserver(this) it produces exception.

Caused by: java.lang.IllegalArgumentException: The observer class has some methods that use newer APIs which are not available in the current OS version. Lifecycles cannot access even other methods so you should make sure that your observer classes only access framework classes that are available in your min API level OR use lifecycle:compiler annotation processor.

Strange, that firstly it was working fine, but not long ago this exception has appeared. I've found, that audioFocusRequest is cause of this bug.

private val audioFocusRequest by lazy {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
        .setOnAudioFocusChangeListener(this)
        .build() else throw RuntimeException("Can't be done for Android API lower than 26")
}

Does anybody know how it can be fixed?

UPD

Tried to use annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version", but got compilation error: enter image description here (decided to paste screenshot, because whole logs are quite big)

UPD 2
At the end I've decided to delete audioFocusRequest field and to use old deprecated method - requestAudioFocus(OnAudioFocusChangeListener l, int streamType, int durationHint) instead of recommended requestAudioFocus(@NonNull AudioFocusRequest focusRequest)

It helped me to make code working again, so it can be solution. But I didn't find answer - why this problem had appeared. It strange because code used to be working before.

So problem has been solved but question still stays unanswered

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Try to use kapt "androidx.lifecycle:lifecycle-compiler:2.0.0"

over 4 years ago · Santiago Trujillo Denunciar

0

The class which implements LifecycleObserver has some method, which has parameters with type that only exist for higher APIs.

Your variables (i guess) and function parameters must exist on all APIs even function is not called (maybe this is requirement for classes who implement LifecycleObserver).

A possible solution is to change function parameter type to Any (kotlin) or Object (Java) and cast it to appropriate type inside function.

over 4 years ago · Santiago Trujillo Denunciar

0

I have to remove this set method on SpinnerView: lifecycleOwner = viewLifecycleOwner

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda