Amisier
Amisier

Reputation: 1

Error in Android Tutorial with sendMessage (view; View) - App Basics, starts another activity

When I try to change the property of the Button (onClick) no the drop downlist is empty and I receive errors when adding the sendMessage (view; View) Method. See below the assigment:

*In the file app > java > com.example.myfirstapp > MainActivity, add the sendMessage() method stub as shown below:

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) {
    // Do something in response to button
  }
}

You may see an error because Android Studio cannot resolve the View class used as the method argument. So click to place your cursor on the View declaration, and then perform a Quick Fix by pressing Alt + Enter (or Option + Enter on Mac). (If a menu appears, select Import class.)

Now return to the activity_main.xml file to call this method from the button: Click to select the button in the Layout Editor. In the Attributes window, locate the onClick property and select sendMessage [MainActivity] from the drop-down list (THIS DOES NOT APPEAR??). Now when the button is tapped, the system calls the sendMessage() method.*

Upvotes: 0

Views: 953

Answers (3)

unielain
unielain

Reputation: 1

I had the same problem with java version of the tutorial. (That's how I found this thread.) Just in case someone else runs in this problem:

  1. I wrote a "system.out.print("Tadaa!")" //a filler code inside the sendMessage method.
  2. crtl + s (=save the file)
  3. try clicking some other area in layout editor and then return to the button-widget again. You should be able to find the sendMessage in onClick-attribute. If not, maybe saving the project, closing it and opening it again would help?

Upvotes: 0

Arghyadip Das
Arghyadip Das

Reputation: 1

You need to write the sendMessage function in Jave syntax and that's it

Try replacing the sendMessage with the code below and then you should be able to view the senMessage

public void sendMessage(View view) {
}

Upvotes: 0

Matthew Jones
Matthew Jones

Reputation: 51

For those that come back to this, try switching to JAVA instructions rather than KOTLIN, this then worked for me!

Upvotes: 1

Related Questions