Skip to content

Commit

Permalink
Fix StripeEditText crash on instantiation (#3905)
Browse files Browse the repository at this point in the history
In `androidx.appcompat:appcompat:1.4.0-alpha02`,
`addTextChangedListener()` is called by a `StripeEditText` superclass.

This was causing a crash because `textWatchers` was not instantiated
before it was accessed. Change `textWatchers` to nullable and
initialize it in `StripeEditText`'s `init` block.

Fixes #3770
  • Loading branch information
mshafrir-stripe committed Jun 30, 2021
1 parent aeab895 commit cbdcb90
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ open class StripeEditText @JvmOverloads constructor(
@ColorInt
private var externalErrorColor: Int? = null

private val textWatchers = mutableListOf<TextWatcher>()
private var textWatchers: MutableList<TextWatcher>? = null

/**
* Gets whether or not the text should be displayed in error mode.
Expand Down Expand Up @@ -96,6 +96,7 @@ open class StripeEditText @JvmOverloads constructor(
}

init {
textWatchers = mutableListOf()
maxLines = 1

listenForTextChanges()
Expand Down Expand Up @@ -264,29 +265,34 @@ open class StripeEditText @JvmOverloads constructor(
@VisibleForTesting
internal fun getParentOnFocusChangeListener() = super.getOnFocusChangeListener()

/**
* Note: [addTextChangedListener] will potentially be called by a superclass constructor
*/
override fun addTextChangedListener(watcher: TextWatcher?) {
super.addTextChangedListener(watcher)

watcher?.let {
textWatchers.add(it)
textWatchers?.add(it)
}
}

override fun removeTextChangedListener(watcher: TextWatcher?) {
super.removeTextChangedListener(watcher)

watcher?.let {
textWatchers.remove(it)
textWatchers?.remove(it)
}
}

/**
* Set text without notifying its corresponding text watchers.
*/
internal fun setTextSilent(text: CharSequence?) {
textWatchers.forEach {
textWatchers?.forEach {
super.removeTextChangedListener(it)
}
setText(text)
textWatchers.forEach {
textWatchers?.forEach {
super.addTextChangedListener(it)
}
}
Expand Down

0 comments on commit cbdcb90

Please sign in to comment.