DisplayMessageActivity MyFirstApp

Hi,

I’m learning both kotlin and android with android tutorials on android developer site. (https://developer.android.com/training/basics/firstapp/26)
I tried writing ‘build your first app’ in kotlin. but I could not refer main activity’s properties from another activity.
here are my codes.

package systems.cyberage.myfirstapp
// package com.example.my.mynewapp
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
// import kotlinx.android.synthetic.res.layout.activity_main.editText

class DisplayMessageActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_display_message)
        val intent: Intent = getIntent()
        val message: String = intent.getStringExtra(MainActivity.EXTRA_MESSAGE) // this does not work: Unresolved reference
        val message: String = "com.example.mynewapp.MESSAGE" // this works
        textView.text = message
    }
}

And My MainActivity.kt is as follow

package systems.cyberage.myfirstapp

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
// import android.provider.AlarmClock.EXTRA_MESSAGE
import android.view.View
import kotlinx.android.synthetic.main.activity_main.editText

class MainActivity : AppCompatActivity() {
companion object {
const val EXTRA_MESSAGE : String = “MyFirstApp.MESSAGE”
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun sendMessage(view: View) {
val intent = Intent(this@MainActivity, DisplayMessageActivity::class.java)
// val message:String = intent.getStringExtra(MainActivity().EXTRA_MESSAGE)
val message = editText.text.toString()
intent.putExtra(EXTRA_MESSAGE, message)
startActivity(intent)
}
}

Can anyone help me out on this. Just couldn’t get it going.

Many Thanks,
John

It looks like you’re missing an import:

import systems.cyberage.myfirstapp.MainActivity

Normally, Android Studio should show MainActivity in red if the import is missing. If you put the cursor on it and press ALT+ENTER (or OPTION+ENTER on Mac) it should offer to import it for you. Alternatively, if you use autocomplete while typing, it will automatically import it as it autocompletes.

By the way, if you write
```Kotlin

on a line by itself before your code and
```

on a line by itself after your code, you will get a nicely formatted code block with syntax highlighting in your post.

VERY GOOD it work.