Cannot find R.layout.activity_main

AndroidAndroid Layout

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

Check if you not imported android.R accidentally.

Solution 3 - Android

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.

Solution 4 - Android

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.

Solution 5 - Android

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.

Solution 6 - Android

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" >

Solution 7 - Android

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

Solution 8 - Android

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

Solution 9 - Android

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

Solution 10 - Android

You're importing invalid R class, check imports.

Solution 11 - Android

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

Solution 12 - Android

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

Solution 13 - Android

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.

Solution 14 - Android

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

Solution 15 - Android

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"

Solution 16 - Android

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

Solution 17 - Android

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.

Solution 18 - Android

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.

Solution 19 - Android

This solved my problem,

apt-get install ia32-libs

and restart :)

https://stackoverflow.com/questions/13571145/android-adb-not-found

Solution 20 - Android

or you can write

import.R.layout // at the top

It will work for sure

Solution 21 - Android

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"

Solution 22 - Android

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.

Solution 23 - Android

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

Solution 24 - Android

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.

Solution 25 - Android

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

Solution 26 - Android

Happened once. Don't know the reason.

Build> Clean Project removed the error for me.

Solution 27 - Android

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

Solution 28 - Android

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

Solution 29 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTheRookierLearnerView Question on Stackoverflow
Solution 1 - AndroidrosscooperView Answer on Stackoverflow
Solution 2 - AndroidArturSkowronskiView Answer on Stackoverflow
Solution 3 - AndroidAbdul WahidView Answer on Stackoverflow
Solution 4 - AndroidWajidView Answer on Stackoverflow
Solution 5 - AndroidRubber DuckView Answer on Stackoverflow
Solution 6 - AndroidTedView Answer on Stackoverflow
Solution 7 - Androidarpit1714View Answer on Stackoverflow
Solution 8 - AndroidHimesh goswamiView Answer on Stackoverflow
Solution 9 - AndroidLeon RomView Answer on Stackoverflow
Solution 10 - AndroidartemView Answer on Stackoverflow
Solution 11 - AndroidSanjayView Answer on Stackoverflow
Solution 12 - AndroidoiyioView Answer on Stackoverflow
Solution 13 - AndroidBudhdi SharmaView Answer on Stackoverflow
Solution 14 - AndroidZeshan ArshadView Answer on Stackoverflow
Solution 15 - AndroidEssamSoftView Answer on Stackoverflow
Solution 16 - AndroidEmil DavtyanView Answer on Stackoverflow
Solution 17 - AndroidSunil KumarView Answer on Stackoverflow
Solution 18 - AndroidTheRookierLearnerView Answer on Stackoverflow
Solution 19 - AndroidDeepView Answer on Stackoverflow
Solution 20 - Androiduser3718327View Answer on Stackoverflow
Solution 21 - AndroidEsmaeil MIRZAEEView Answer on Stackoverflow
Solution 22 - AndroidAndrew B.View Answer on Stackoverflow
Solution 23 - AndroidGK android beginnerView Answer on Stackoverflow
Solution 24 - AndroidMKHView Answer on Stackoverflow
Solution 25 - AndroidTvsView Answer on Stackoverflow
Solution 26 - Androidkunal agarwalView Answer on Stackoverflow
Solution 27 - AndroidVladView Answer on Stackoverflow
Solution 28 - AndroidBukunmi View Answer on Stackoverflow
Solution 29 - AndroidNehil KoshiyaView Answer on Stackoverflow