TheRookierLearner TheRookierLearner - 2 days ago 5
Android Question

Cannot find R.layout.activity_main

I was trying to work out with multiple layout using some online tutorials. The thing is my program was building and compiling correctly as long as there was only one XML file. When I added multiple XML files, I am getting errors saying 'activity_main cannot be resolved or is not a field' for the line

setContentView(R.layout.activity_main);


In fact, every item which I used previously in
R.*
is now reporting error. I don't know what went wrong. Can anyone suggest what needs to be done?

Edit 1: I tried cleaning and rebuilding the project but that didn't help. Plus, I am importing the correct
R.java
file as in mypackage.R.java

According to @RubberDuck's comment, I as soon as I deleted the files again, the R.java file was being generated after building the project. However, when I create new XML files and add elements in it, build the project, R.java gets deleted.

Answer

If your XML's contain errors your generated file might not build try erasing most of their content and making sure id's and file names are not duplicated and no capitals and spaces are used. If it works you can start adding stuff.

Comments