I want to make extension function using TextInputEditText and call addTextChangedListener, but when i run the app and using the feature, my app crash. My goal is to create an extension function addTextChangedListener for the TextInputEditText type so that the length of the code can be more concise
private fun TextInputEditText.textChangedListener() {
addTextChangedListener(object: TextWatcher{
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun afterTextChanged(p0: Editable?) {
findViewById<TextInputLayout>(R.id.regisPasswordLayout).error = null
findViewById<TextInputLayout>(R.id.regisConfiPasswordLayout).error = null
findViewById<TextInputLayout>(R.id.regisNamaLayout).error = null
findViewById<TextInputLayout>(R.id.regisEmailLayout).error = null
}
})
}
How I call it
inputPassword.textChangedListener()
inputPasswordConfirmation.textChangedListener()
inputNama.textChangedListener()
inputEmail.textChangedListener()
error logcat which contains my activity
2022-08-31 07:50:45.653 16776-16776/com.kotlin.cloudstorage E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kotlin.cloudstorage, PID: 16776
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.textfield.TextInputLayout.setError(java.lang.CharSequence)' on a null object reference
at com.kotlin.cloudstorage.RegisterActivity$textChangedListener$1.afterTextChanged(RegisterActivity.kt:132)
at android.widget.TextView.sendAfterTextChanged(TextView.java:10594)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:13581)
at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1277)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:577)
at androidx.emoji2.text.SpannableBuilder.replace(SpannableBuilder.java:315)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:507)
at androidx.emoji2.text.SpannableBuilder.replace(SpannableBuilder.java:305)
at androidx.emoji2.text.SpannableBuilder.replace(SpannableBuilder.java:49)
at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:228)
at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:141)
at androidx.emoji2.viewsintegration.EmojiKeyListener.onKeyDown(EmojiKeyListener.java:58)
at android.widget.TextView.doKeyDown(TextView.java:8466)
at android.widget.TextView.onKeyDown(TextView.java:8240)
at android.view.KeyEvent.dispatch(KeyEvent.java:2842)
at android.view.View.dispatchKeyEvent(View.java:14247)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1959)
at com.android.internal.policy.DecorView.superDispatchKeyEvent(DecorView.java:476)
at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1861)
at android.app.Activity.dispatchKeyEvent(Activity.java:4085)
at androidx.core.app.ComponentActivity.superDispatchKeyEvent(ComponentActivity.java:124)