Skip to content
Advertisement

Android onclick listeners not recognized in drawer/sidebar template

On creating a new project in android studio, I chose the drawer template:

Navigation Drawer Activity

I began working on my project and in the XML file (fragment_home.xml), I added an onclick listener to a button in the fragment_home.xml All the code of fragment_home.xml

JavaScript

Anf then… I went to the MainActivity.java file and added two lines:

JavaScript

So this is how the MainActivity.java file looks like

JavaScript

*Note most of the code is built in… all I add were the two lines before the last closing bracket. Those are the only 2 files I changed. All the other files are untouched. But for some reason, in the fragment_home.xml, I get a warning:

enter image description here

Did I do something wrong, I clearly added it in the MainActivity.java. What’s going on here?

Advertisement

Answer

doSomething should be declared in the file which inflates fragment_home.xml i.e. HomeFragment.java and not inside MainActivity.java

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement