[android] 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.

This question is related to android android-layout

The answer is


I've got this when an inappropriate character (/-one slash) was added to the values/strings.xml, and, of course, renaming of problematic resource was helpful. And one more important thing - check the date and time on your device.


I removed import android.R and added import android.support.v7.appcompat.*;

However this had to be done after importing appcompat by right click project and import


or you can write

import.R.layout // at the top

It will work for sure


Ivwd simply removed line "import android.R;" and call Project->Clean... That was enough for my on Win-7/x64 and Eclipse Kepler and last (02.2014) Android SDK


First Doing This --> Select Build > Clean Project from the Android Studio toolbar, wait a few moments, and then build your project by selecting Build > Rebuild Project.

if Problem Not solved then doing This --> File > Invalidate Caches / Restart > Invalidate and Restart from Android Studio’s toolbar.


In my case , i accidentally imported my library's R. class not my app module's R. After editing this, it is resolved.


I used maven as build tool, after I build my project and dig a lot in the internet to solution. My problem resolved by adding to the main_activity.xml

tools:context=".MainActivity"


what I tried:

  1. check every XML file for some famous known errors.
  2. cleaned the project multiple times.
  3. synced Gradle with the project file.

What Worked:

1. Invalidate caches and restart.


The stupid mistake I made to cause this error was to have the wrong package string in my AndroidManifest.xml file. I'd left the string set in the tutorial, but had used my own when setting up the project.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="COM.YOU.YOURAPPNAME"
android:versionCode="1"
android:versionName="1.0" >

Thanks to @RubberDuck's comment and @Emil's answer I was able to figure out what the problem was. The IDs of most elements in my XML file were exactly the same. So, I renamed each and every one of them. Also, my XML file contained capital letters. The filename should be in [a-z0-9_] so I renamed my files too and the problem was solved.


Happened once. Don't know the reason.

Build> Clean Project removed the error for me.


I have a few suggestions:

  1. Make sure you don't have any other errors other than the R-related errors. Right-click your project folder in Eclipse, Android Tools -> Fix Project Properties.

    2.Check to make sure you have the correct R imported. Sometimes the default Android.R can be imported.

    3.Check for errors in your layout XML files.


I had a similar problem.
It happened suddenly, after deleting some pictures from res. drawable. I did two things.

  • First I checked all my images in the drawable folder,if they all have preview. one of mine was damaged.
  • Secondly, I closed all opened android studio and emulator. After that every things worked fine.

Check if you not imported android.R accidentally.


You're importing invalid R class, import yourpackage.R class for example com.example.R

what actully happends that u import android.R class not yourpackages.R


My error got removed by importing the line

import com.example.myapplication.R;

your import line may be different depending upon your project name in which my project name is myapplication


There is probably an issue with the layout files, correct it and the auto generated class will appear.


it happens when you change the project name

in my case, I just change

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="xxxx.newName"

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

This solved my problem,

apt-get install ia32-libs

and restart :)

Android adb not found


You're importing invalid R class, check imports.


After cleaning the project and syncing with gradle(no result), i removed all my import statement, and brought them back in using alt+Enter.


This is happening due to resources are not loading properly and the similar issue can occur any view also so Below are some technics which you can use to remove this error:-

  • Clean your project, Build -> clean Project
  • Rebuild project, Build -> Rebuild Project
  • Open manifest and check if there's any resource missing
  • Check-in layout to particular id if missing add it
  • If the above steps did not work then restart android studio with invalidating the cache.

Note: In the above-mentioned steps, the last step will work for this issue for sure.


In Android Studio go to Tools -> Android -> Sync project with gradle files or File -> Sync project with gradle files in latest Android Studio version.


in 2019 I faced the same problem and I searched the internet and found the following which I am sharing with all.

In android studio letter R stands for the resources and this error occurs because of the build process not able to sync resources with your project. In other words, this error is caused when Android Studio can’t generate your R.java file correctly. This problem happens when you shift code to another system or while building the android project for the first time. So when you create a new activity or new class you will see an error message like “cannot resolve symbol r” with a red underline.

Below you can find the possible ways to fix cannot resolve symbol r in android studio.

Update Project Gradle To Latest Version Always use the latest version of Gradle to work android studio properly.

Sync Project With Gradle File Once you update the Gradle plugin you need to sync project with the Gradle file. Open android studio and click on Files > Sync Project with Gradle Files option.

Clean and Rebuild Project The most effective solution is the simplest: clean and rebuild your project. Select Build > Clean Project from the Android Studio toolbar, wait a few moments, and then build your project by selecting Build > Rebuild Project.

Invalidate Caches / Restart If you encounter this error after moving some files and directories around, then it’s possible that the R.layout error is being caused by a mismatch between Android Studio’s cache and your project’s current layout. If you suspect this may be the case, then select File > Invalidate Caches / Restart > Invalidate and Restart from Android Studio’s toolbar. Issues with the names of your resources can also prevent the R.java file from being created correctly, so check that you don't have multiple resources with the same name and that none of your file names contain invalid characters. Android Studio only supports lowercase a-z, 0-9, full stops and underscores, and a single invalid character can cause an R.layout error across your entire project, even if you don’t actually use this resource anywhere in your project!

My problem and its solution: In my case, I applied all the above but could not solve the problem. Thus I started a new project and pasted my code one by one and validated my code with running the app. Finally, at one point when I first deleted the code in colors.xml and copied and pasted code below in colors.xml file, I got the error.

<color name="bg_login">#26ae90</color>
<color name="bg_register">#2e3237</color>
<color name="bg_main">#428bca</color>
<color name="white">#ffffff</color>
<color name="input_login">#222222</color>
<color name="input_login_hint">#999999</color>
<color name="input_register">#888888</color>
<color name="input_register_bg">#3b4148</color>
<color name="input_register_hint">#5e6266</color>
<color name="btn_login">#26ae90</color>
<color name="btn_login_bg">#eceef1</color>
<color name="lbl_name">#333333</color>
<color name="btn_logut_bg">#ff6861</color>

when I undo my changes the error vanished again. Thus my code in colors.xml is not the code above and the code already in colors.xml i.e

<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="bg_login">#26ae90</color>
<color name="bg_register">#2e3237</color>
<color name="bg_main">#428bca</color>
<color name="white">#ffffff</color>
<color name="input_login">#222222</color>
<color name="input_login_hint">#999999</color>
<color name="input_register">#888888</color>
<color name="input_register_bg">#3b4148</color>
<color name="input_register_hint">#5e6266</color>
<color name="btn_login">#26ae90</color>
<color name="btn_login_bg">#eceef1</color>
<color name="lbl_name">#333333</color>
<color name="btn_logut_bg">#ff6861</color>

"Maybe its because i did not have color primary defined" Hope it will help someone like me who is new in programming.


JUST REMOVE the import.android.R;

Then the R.layout.(XML LAYOUTNAME) will turn to BLUE

IT TOOK ME HOURS TO FIND THIS ONE ERROR


goto tools->android->Sync project with gradle file then click on S


Either your gradle or build need to know where resources are.