Home > OS >  Getting the Text of an EditText outside of the main activity
Getting the Text of an EditText outside of the main activity

Time:05-31

I just got into developing Android apps with Kotlin and run into the following Problem: I wanted to build a small login just for fun. I kinda figured that that is due to me trying to get the Text via v.findViewItemById() but can't find another way/a way to go around it
But when executing the App crashes on me with the error
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

My MainActivity.kt: (removed imports)


class MainActivity : AppCompatActivity(){
    private lateinit var B: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        B = ActivityMainBinding.inflate(layoutInflater)
        setContentView(B.root)


        val btnListener = ButtonClickListener()
        B.btnLogin.setOnClickListener(btnListener)
    }
}

And the ButtonClickListener.kt:

package de.fynnhenck.login.listener

import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import de.fynnhenck.login.MainActivity
import de.fynnhenck.login.R
import java.security.AccessController.getContext

class ButtonClickListener : View.OnClickListener {
    override fun onClick(v: View?) {
        val bu = v as Button
        val loginText = v.findViewById<TextView>(R.id.tv_loginText)
        val username = v.findViewById<EditText>(R.id.et_username)

        val password = v.findViewById<EditText>(R.id.et_password)

            if(username.text.equals("user") and password.text.equals("pass")){
                loginText.text = "Test"
            }
    }
}

I hope this explains my issue well enough. Thank ya'll for your help in advance :)

CodePudding user response:

I believe its crashing because onClick(v: View?) only contains view of your button and not your 2 other editText and thats you are getting null.

Why two files and not directly adding this logic in MainActivity?

B.btnLogin.setOnClickListener {
   if(B.username.text.equals("user") && B.password.text.equals("pass")){
      B.loginText.text = "Test"
    }
 }

CodePudding user response:

When you call findViewById of your View (btnLogin is your case), it tries finding between descending views. So since tv_loginText is not child view of your button, this row

val loginText = v.findViewById<TextView>(R.id.tv_loginText)

sets loginText to null.

Try accessing your textviews via binding:

class MainActivity : AppCompatActivity(){
    private lateinit var B: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        B = ActivityMainBinding.inflate(layoutInflater)
        setContentView(B.root)


        val btnListener = ButtonClickListener()
        B.btnLogin.setOnClickListener(btnListener)
    }

    class ButtonClickListener : View.OnClickListener {
        override fun onClick(v: View?) {
            val bu = v as Button


            val loginText = B.tvLoginText // LIKE THIS
            ...
    }
}
  • Related