'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.*
Solution 1:[1]
For those that come back to this, try switching to JAVA instructions rather than KOTLIN, this then worked for me!
Solution 2:[2]
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) {
}
Solution 3:[3]
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:
- I wrote a "system.out.print("Tadaa!")" //a filler code inside the sendMessage method.
- crtl + s (=save the file)
- 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?
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 | Matthew Jones |
Solution 2 | Arghyadip Das |
Solution 3 | unielain |