Wackaloon
Wackaloon

Reputation: 2365

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?

Upvotes: 3

Views: 1263

Answers (1)

Onik
Onik

Reputation: 19949

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 = ...

Upvotes: 3

Related Questions