0

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)
        
2
  • 2
    Hello, Please avoid using images to present code. Add the code to the question using the code block option. Also, please provide the crash log. Commented Aug 30, 2022 at 14:02
  • when you have the crash it is important to read the crash stacktrace as it usually points a finger at what is wrong Commented Aug 30, 2022 at 14:19

1 Answer 1

0

The problem is in findViewById, because your TextInputEditText doesn't containt TextInputLayout also your TextInputEditText can't contains nested view

Sign up to request clarification or add additional context in comments.

1 Comment

Can you give me some example how to implements it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.