Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 779
TheRookierLearner Avatar asked Apr 16 '13 19:04

TheRookierLearner


2 Answers

I had the same problem, fixed by replacing R with com.example.appname.R obviously put your package reference in there instead. Or just add this line to your file:

import com.your.package.R 

Or even better, try removing this line from your code if exists:

import android.support.compat.R 
like image 81
rosscooper Avatar answered Oct 21 '22 07:10

rosscooper


Check if you not imported android.R accidentally.

like image 43
ArturSkowronski Avatar answered Oct 21 '22 07:10

ArturSkowronski