NPE when using setText in EditText using PhoneNumberFormattingTextWatcher?

Background

I have an EditText that is designed to put a phone number in it. So I used this function on it:

mPhoneNumberEditText.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

      

In some cases I need to put in the phone number myself, so I use setText (...) on it (with a valid phone number of course, but that should also be possible with an invalid phone number too).

Problem

I found that on some Android devices (rather unpopular ones that also have versions for Lollipop Android) setting text may result in the following exception. This is quite rare:

Caused by: java.lang.NullPointerException
       at android.telephony.PhoneNumberFormattingTextWatcher.reformat(PhoneNumberFormattingTextWatcher.java:158)
       at android.telephony.PhoneNumberFormattingTextWatcher.afterTextChanged(PhoneNumberFormattingTextWatcher.java:135)
       at android.widget.TextView.sendAfterTextChanged(TextView.java:7626)
       at android.widget.TextView.setText(TextView.java:3859)
       at android.widget.TextView.setText(TextView.java:3708)
       at android.widget.EditText.setText(EditText.java:81)
       at android.widget.TextView.setText(TextView.java:3683)

      

What i tried

I tried to get the PhoneNumberFormattingTextWatcher code and see where it crashes, but for some reason the lines don't line up with the crash report lines.

I also tried to get the code to make sure it is used, but it has some special classes and functions that are not available (for example, "com.android.i18n.phonenumbers.AsYouTypeFormatter" and "com.android.i18n.phonenumbers.PhoneNumberUtil" ).

Question

How can I handle this exception? Why is this happening?

Is it possible that some companies have changed the code and this is causing problems?

+3


source to share





All Articles