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.
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
Check if you not imported android.R accidentally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With