Hi.
I am starting to learn Kotlin through developer.Android and while copy/pasting its java code to android studio and
following the exact instructions still I got error when I ran my app on emulator. The first activity runs fine but when I click on send button to go to next activitiy to display the msg from the first activity it says app has stopped working. i am pasting my code below
package madinah.jafferdigital.com.starterproject
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.appcompat.R.styleable.View
import android.view.View
import android.provider.AlarmClock.EXTRA_MESSAGE
import android.widget.EditText
import android.content.Intent
import android.view.Display
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun sendMessage(view: View) {
val intent = Intent(this, Display::class.java)
val editText = findViewById<View>(R.id.editText) as EditText
val message = editText.text.toString()
intent.putExtra(EXTRA_MESSAGE, message)
startActivity(intent)
}
}
<?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=“madinah.jafferdigital.com.starterproject.MainActivity”>
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/edit_message"
android:inputType="text|textPersonName"
app:layout_constraintEnd_toStartOf="@+id/button1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:onClick="sendMessage"
android:text="@string/button_send"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editText"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
package madinah.jafferdigital.com.starterproject
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 madinah.jafferdigital.com.starterproject.R.id.editText
import android.widget.TextView
class DisplayMessageActivity : AppCompatActivity() {
class MainActivity : AppCompatActivity() {
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun sendMessage(view: View) {
val intent = Intent(this, DisplayMessageActivity::class.java)
val message = editText.toString()
intent.putExtra(EXTRA_MESSAGE, message)
startActivity(intent)
val textView = findViewById<TextView>(R.id.textView)
textView.text = message
}
}
=======================================================================
activity_display_message.xml
<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=“madinah.jafferdigital.com.starterproject.DisplayMessageActivity”>
<TextView
android:id="@+id/textView"
android:layout_width="145dp"
android:layout_height="39dp"
android:layout_marginEnd="208dp"
android:layout_marginStart="208dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textColor="@android:color/black"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>