Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

597
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!