Why I'm getting a null string through Intent even after passing the value?
MainActivity
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding=ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btnFirst.setOnClickListener {
val name=binding.etName.text.toString()
Intent(this,SecondActivity::class.java).also{
it.putExtra("EXTRA_NAME",name)
startActivity(it)
}
}
}
}```
SecondActivity
```class SecondActivity:AppCompatActivity() {
private lateinit var binding: ActivitySecondBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding= ActivitySecondBinding.inflate(layoutInflater)
setContentView(binding.root)
val name=intent.getStringArrayExtra("EXTRA_NAME")
val toPrint="$name hahahaha"
binding.tvNameIntent.text=toPrint
}
}
toPrint is getting "null hahahaha"
Can someone please rectify my error
CodePudding user response:
You are putting in a String
so you should be also reading a String
and not StringArray
. You can use e.g. getStringExtra()
for that.
CodePudding user response:
val name=intent.getStringArrayExtra("EXTRA_NAME") should be changed to val name=intent.getStringExtra("EXTRA_NAME")