0

I can't call function of my class from object inside this class. How should i do this ?

 class LoginActivity: AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_login)
        }

     private fun disableLoginButton(){
            button_login.isEnabled = false
     }

     private object textChangeListener: TextWatcher{

            override fun afterTextChanged(p0: Editable?) {
            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                //Here i cannot call function
                disableLoginButton() // unresolved reference. 
            }

        }
    }

But when i call LoginActivity().disableLoginButton() instead disableLoginButton()it's visible, but fails with

NullPointerException

on login_button

1
  • Inner objects are singletons and cannot access wrapping class instance. I have added details to my wrong answer Commented Jan 28, 2020 at 12:24

2 Answers 2

2

Try this :

    class LoginActivity: AppCompatActivity(){
        override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
                setContentView(R.layout.activity_login)

        editTextSample.addTextChangedListener(object : TextWatcher {

                override fun afterTextChanged(s: Editable) {}

                override fun beforeTextChanged(s: CharSequence, start: Int,
                                               count: Int, after: Int) {
                }

                override fun onTextChanged(s: CharSequence, start: Int,
                                           before: Int, count: Int) {
                    disableLoginButton()
                }
            })

            }




        }

     private fun disableLoginButton(){
                button_login.isEnabled = false
         }
Sign up to request clarification or add additional context in comments.

5 Comments

No, button_login doesn't visible too.
What you have to do, you want to hide or visible button Or disable click event ?
please paste your full activity here
i just want to access my button from TextWatcher object, to disable it, when user starts writing on editText.
Just apply TextWatcher on your UI component , here I mentioned editTextSample
0

EDIT: Doesn't work

From jetbrains team:

In Java and Kotlin, "inner" means "capturing the outer instance", where as "nested" means simply declared inside something else. Java's static classes are only nested, non-static nested classes are inner. In Kotlin you have to explicitly declare something as "inner" (we reversed the Java's convention). So, your object is not inner, btu only nested. And no named object can be inner, in fact: named objects are singletons, so the can not depend on any kind of outer instance.

Try specifying the object as inner:

private inner object textChangeListener: TextWatcher{

     override fun afterTextChanged(p0: Editable?) {
     }

    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
     }

     override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int {
          //Here i cannot call function
           disableLoginButton() // unresolved reference. 
     }
}

This should allow you to access the outer scope.

1 Comment

I am sorry, I believed all inner declarations were supposed to be marked as inner explicitly

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.