'Beginning Android programming - Build your first app

After just installing the latest Android Studio (3.0.1) I started with the Build Your First App guide. I'm using Kotlin as it seems to be the suggested way to go. I managed to run the first Hello World app on my tablet. Then, continuing with the example at https://developer.android.com/training/basics/firstapp/starting-activity.html#kotlin

I got to the point where it instructs "In the Attributes window, locate the onClick property and select sendMessage [MainActivity] from the drop-down list." The problem is, there is nothing in the drop-down list.

I tried typing sendMessage manually in the onClick property of the button, but then the app crashes when I push the button. Also tried sendMessage() , sendMessage(this) and sendMessage [MainActivity] but all of them crash the app. If I remove it, pushing the button does - quite expectedly - nothing, but does not crash either... Also tried adding the property into the .xml as instructed in another reply, but no help.

So what is the correct way to call the sendMessage function? Which View object (?) should I give it and how?

My MainActivity looks like this:

package com.example.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)
    }

    /** Called when the user taps the Send button */
    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)
    }
}

and the DisplayMessageActivity:

package com.example.myfirstapp

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 its text
    val textView = findViewById<TextView>(R.id.textView).apply {
        text = message
    }
}

And I'll raise with the whole stack:

03-13 10:19:50.620 20646-20646/com.example.myfirstapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.example.myfirstapp, PID: 20646
                                                                        java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2488)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665)
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                            at android.os.Looper.loop(Looper.java:207)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5765)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
                                                                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
                                                                            at com.example.myfirstapp.DisplayMessageActivity.<init>(DisplayMessageActivity.kt:15)
                                                                            at java.lang.Class.newInstance(Native Method)
                                                                            at android.app.Instrumentation.newActivity(Instrumentation.java:1072)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2478)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665) 
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                            at android.os.Looper.loop(Looper.java:207) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5765) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 


Solution 1:[1]

Your sendMessage method is right.

Typing "sendMessage" in the onClick field(the method name) is enough.

Or in the xml, add

android:onClick="sendMessage"

to the button attributes.

Solution 2:[2]

Either you can add android:onClick="sendMessage" or add setOnClickListener in your MainActivity.

YourButtonID.setOnClickListener { v -> sendMessage(v) }

Solution 3:[3]

There are 2 issues.

1. The "there is nothing in the drop-down list"

It's 2022 and I faced the same issue. To solve it, I did nothing, just took a break, when I got back the sendMessage shows up in the drop-down list. I guess it's the IDE. You can try ctrl-S in the MainActivity or restart the IDE.

2. Misplaced when copy-pasting this code which results in the above stacktrace

From the docs:

In DisplayMessageActivity, add the following code to the onCreate() method:

"to the" means inside:

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 its text
    val textView = findViewById<TextView>(R.id.textView).apply {
        text = message
    }
}

If we put the code outside of the onCreate() method, the app will crash after we tap the button like OP mentioned.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dewey Reed
Solution 2 esu
Solution 3