简体   繁体   中英

passing data from an activity to another and getting an error when passing and EditText

I'm trying to pass a string in an EditText from LoginActivity to TwoActovity and this is the code:

LoginActivity (when a button is clicked)

var userName: String = editText.toString()

val i = Intent(this@LoginActivity, TwoActivity::class.java)
i.putExtra("userNamePass", userName)
startActivity(i)
}

TwoActivity

class TwoActivity : AppCompatActivity() {
    lateinit var userNamePassed: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_two)

        var int:Intent = intent
        userNamePassed = int.getStringExtra("userNamePass")
        textView10.text = userNamePassed


    }
}

this is the error message I get after entering a name in LoginActivity and clicking the button (it goes to TwoActivity but shows this message insted of the name I actually typed in LoginAvtivity): enter image description here

in the LoginActivity if I passed "any string in double quotation" instead if userName like the following code it works

//var userName: String = editText.toString()

val i = Intent(this@LoginActivity, TwoActivity::class.java)
i.putExtra("userNamePass", "anything")
startActivity(i)
}

First of all, you're not getting an error, it's just string value of your EditText because you said editText.toString() .

What you really want is editText.text.toString() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM