Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

273
Visualizações
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 Respostas
Responde à pergunta

0

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

over 4 years ago · Santiago Trujillo Relatório

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 Relatório

0

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda