1

The MainActivity.kt recieve the latitude and longtitude

val intent = Intent(this@MainActivity,MapsActivity::class.java)
        intent.putExtra("latitude", latitude)
        intent.putExtra("longitude", longitude)

And then pass to MapsActivity.kt

val intent = Intent(this@MapsActivity,MainActivity::class.java)
    val lat=intent.getStringExtra("latitude").toDouble()
    val lon=intent.getStringExtra("longtitude").toDouble()

And when I run the application I get a errors while I go to MapsActivity enter image description here enter image description here enter image description here

What is the main cause of this problem and how can I pass the values in the correct way?

  • You create a second `Intent` object and expect to retrieve data from the first with it. https://stackoverflow.com/questions/3913592/start-an-activity-with-a-parameter – Benoit Guina Apr 18 '20 at 13:19

1 Answers1

2

Set

val intent = Intent(this@MainActivity,MapsActivity::class.java)
intent.putExtra("latitude", latitude!!)
intent.putExtra("longitude", longitude!!)
startActivity(intent)

Get

var bundle :Bundle ?=intent.extras
var latitude = bundle!!.getString("latitude").toDouble()
var longitude = bundle!!.getString("longitude").toDouble()
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198