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

599
Visualizações
Kotlin: EditText is null in afterTextChanged()

I have an EditText and I'm setting an error into it or dismiss the error after text changes. However somehow I'm getting NPE when trying to access the EditText from the afterTextChanged() method.

phone_number_input.addTextChangedListener(object : TextWatcher() {
        ...
        override fun afterTextChanged(s: Editable?) {
            if (isValid(s.toString()) 
                phone_number_input.error = null // <-- NPE happens here
            else
                phone_number_input.error = "Number is invalid"
        }
    })

It's not reproducing constantly, but for the last month there were dozens of crashes on different devices starting from Android 4.4.2 up to 6.0.1.

How can that happen? If Fragment is destroyed, TextWatcher shouldn't be called, right? How can it be prevented?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

How can that happen?

Most likely, when this happens your app goes to foreground while user is typing (e.g. due to an incoming call).

If Fragment is destroyed, TextWatcher shouldn't be called, right?

Right. But you're missing the order in which Fragment and Layout inflated "within" it get destroyed. The destruction for those two aren't done simultaneously - Layout gets destroyed first.

As you can see, the TextWatcher is an anonymous inner class instance that keeps reference to its outer class, your Fragment, which is to be destroyed last. The key point here is so that any text changes within EditText coming from the TextWatcher are done asynchronously - your app's process view gets "notifications" from another process in the system, soft keyboard app (default one).

In case when such a "notification" comes at a time your EditText has been destroyed but your Fragment has not, you get NPE.

How can it be prevented?

Simply use phone_number_input?.error = ...

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