Reputation: 23
I'm new to AS and i'm having some teething issues.
I am following this tutorial with the kotlin path https://developer.android.com/training/basics/firstapp/
I am getting these errors when attempting to run the app
e: C:\Users\Dan\Downloads\Whackamole-Making-toast-v1.0\MyFirstApp\app\src\main\java\com\example\dan\myfirstapp\DisplayMessageActivity.kt: (19, 52): Unresolved reference: textView
e: C:\Users\Dan\Downloads\Whackamole-Making-toast-v1.0\MyFirstApp\app\src\main\java\com\example\dan\myfirstapp\MainActivity.kt: (20, 22): Expression 'intent' of type 'Intent!' cannot be invoked as a function. The function 'invoke()' is not found
e: C:\Users\Dan\Downloads\Whackamole-Making-toast-v1.0\MyFirstApp\app\src\main\java\com\example\dan\myfirstapp\MainActivity.kt: (21, 13): Unresolved reference: putExtra
As far as I can tell I have entered the code exactly as it should be. Any help is greatfully appreciated
EDIT: Heres the code
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myfirstapp.MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/edit_message"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="@+id/button"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:onClick="sendMessage"
android:text="@string/button_send"
app:layout_constraintBaseline_toBaselineOf="@+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/editText" />
</android.support.constraint.ConstraintLayout>
MainActivity.kt
package com.example.dan.myfirstapp
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
const val EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun sendMessage(view: View) {
val editText = findViewById<EditText>(R.id.editText)
val message = editText.text.toString()
val intent = intent(this, DisplayMessageActivity::class.java).apply {
putExtra(EXTRA_MESSAGE, message)
}
startActivity(intent)
}
}
activity_display_message.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DisplayMessageActivity">
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
DisplayMessageActivity.kt
package com.example.dan.myfirstapp
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class DisplayMessageActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_display_message)
//Get the Intent that started this activity and extract the string
val message = intent.getStringExtra(EXTRA_MESSAGE)
//Capture the layout's TextView and set the string as it's text
val textView = findViewById<TextView>(R.id.textView).apply {
text = message
}
}
}
Upvotes: 2
Views: 861
Reputation:
textView
does not exist in activity_display_message.xml
val intent = Intent(..)
instead of val intent = intent(..)
For the 1st: place a TextView
with id textView
in activity_display_message.xml
For the 2nd: you instantiate the variable intent
as an instance of the class Intent
.
Edit: I found the lesson you're working on and I see that you missed the part with the title:
"Add a text view"
Upvotes: 2