This is the most common error encountered while working with Android Eclipse ADT or Android Studio IDE. You get this error if R.class file is not generated under gen folder, this file is auto generated every time the build happens, if there is any error encountered while build you get R Cannot Be Resolved to a Variable.
There are many reasons that may cause this error. I am trying to list down as many reasons as I can to my knowledge.

1. R.java Class Import
Check if android.R import statements are present in any of your Java files under src folder, it is not required as R is static class, but many a time we do Ctrl + Shift + O which causes this import statement being added.
Eclipse somethings cries for these imports but just don't add them, the problem is due to some other stuffs like layouts or drawables.
2. Drawables
Drawables (images) or xml's contained here are the real culprits most of the times for such issues. You won't even see a red error cross displayed over the drawable folder (but if you clean and build your project you would find the error messages displayed under logCat console)
Check that images and XML files follows the below mentioned naming conventions,
- All file names must be in lower-cases
- Files under drawables must not contain any special characters other than _ (underscore)
- Files must not contain numeric values.

3. AndroidManifest.xml
If you have changed the package name for your app in the Manifest file, this may not generate R.class resulting in such a error. Make sure you clean and build.
4. Check Order of Export
Right-Click on you project
Project Properties
Java Build Path
Order and Export
5. Check if Android Target is set.
Right-click on your Project Folder.
Select Properties.
Select Android.
Check the box next to appropriate Project Build Target.
Click Apply and OK.
- Increase Android Emulator Timeout time
- Android : Remove ListView Separator/divider programmatically or using xml property
- Error : Invalid key hash.The key hash does not match any stored key hashes
- How to Change Android Toast Position?
- Android Alert Dialog with Checkboxes example
- Android : No Launcher activity found! Error
- Android Development: Spinners with Example
- Failed to sync Gradle project Error:failed to find target android-23
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error
- Disable Fading Edges Scroll Effect Android Views
- How to create Toast messages in Android?
- Channel 50 SMSes received every few minutes Android Phones
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Create Custom Android AlertDialog
- How To Disable Landscape Mode in Android Application
- Android Development - How to switch between two Activities
- incorrect line ending: found carriage return (\r) without corresponding newline (\n)
- Generate Facebook Android SDK keyhash using java code
- Android Error Generating Final Archive - Debug Certificate Expired
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Android RatingBar Example
- 11 Weeks of Android Online Sessions-15-Jun-to-28-Aug-2020
- Download interrupted: Unknown Host dl-ssl.google.com Error Android SDK Manager
- fill_parent vs match_parent vs wrap_content
- Android : Connection with adb was interrupted 0 attempts have been made to reconnect
- Fix: Administrative Privileges Required Error Code: 0-2005 (1223) - Windows-11
- Error : Invalid key hash.The key hash does not match any stored key hashes - Android
- Disable Control Scroll Zoom-in and Zoom-out in Notepad++ - NotepadPlusPlus
- How to stop MongoDB Server running on Ubuntu - Ubuntu
- Ubuntu: How to set Environment Variable - Ubuntu
- How to submit website to dmoz directory - HowTos
- How to read int value using Scanner Class Java - Java
- [Java] Generate Getters and Setters in VS Code - Java