user11971743
user11971743

Reputation:

Pass the values to another activity in Kotlin

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?

Upvotes: 1

Views: 745

Answers (1)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75798

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()

Upvotes: 3

Related Questions