Code2care : TechZone Apps & Tutorials

Android : R Cannot Be Resolved To A Variable - Error Solutions



R Cannot Be Resolved to a Variable
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.
    • Drawables Error resulting in R Not found error

      Even Check if the resources that you have referred in the project are actually present in the res folder.



  • 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.
    • Android Project Build Target





  • 6. Clean and Build the Project!

    • This is the first thing you must try, sometimes the resources are not compiled correctly. Many a times it works Clean and Build. Sometimes Auto-clean option is disabled causing this error.

      Clean and Build ... Also check if Build Automatically is selected



  • 7. Android tools - Fix Project Properties

    • This sometimes works,

    • Right-Click on your Project Folder.
    • Select Android tools - Fix Project Properties.
    • Android tools - Fix Project Properties




    If you have tried all the above solutions and still the issue persists then try restarting eclipse, believe me it works.

    Hope it helps!



    Top


    Posts related to android.


    1. Share image and text Twitter using your Android Application Programatically

    2. The Android Virtual Device 'myEmulator' is currently running an emulator and cannot be deleted.

    3. Make Android View Scrollable both Horizontally and Vertically

    4. Can we move apps like WhatsApp, Facebook to external MicroSD card

    5. Parsing Data for android-21 failed unsupported major.minor version 51.0

    6. Android : Prevent App for rotation landscape or portrait

    7. ADT quit unexpectedly error on Mac OSX Android Eclipse SDK

    8. Android-Failed to install apk on device EOF Timeout Error

    9. How to change Android EditText Cursor Color

    10. JavaDocs for Android SDK Eclipse IDE

    11. Two Buttons next to each other in Android Layout

    12. Change Title text for Android Activity using java code

    13. How to add Newline to text in Android TextView

    14. Android : Unable to load VM from snapshot : Mac OS X Error

    15. INVALID FILE NAME: MUST CONTAIN ONLY [a-z0-9_.] Android Eclipse Error

    16. Hide Tittle bar and Navigation bar from android activity

    17. appcompat_v7 errors after updates to API level 21 Material Theme

    18. How to Copy Text to Android Clipboard Programatically ClipboardManager (Both Old and New SDK)

    19. [Solution] Running Android Lint has encountered a problem NullPointerException Error

    20. Remove Trailing zeros BigDecimal Java

        more...

    Posts related to eclipse.


    1. 27 Code Snippets/Templates to Eclipse IDE Tricks

    2. How to use Content Assist in Eclipse IDE : Tricks

    3. How to enable Spellcheck in Eclipse Workspace

    4. Java JRE Installation location Mac OS X 10.10 Yosemite

    5. Parsing Data for android-21 failed unsupported major.minor version 51.0

    6. Android Launch! The connection to adb is down, and a severe error has occured.

    7. Eclipse like Auto Import Shortcut in Intelij IDE Android Studio

    8. JavaDocs for Android SDK Eclipse IDE

    9. [Solutions] Android Error in an XML file: aborting build. Eclipse SDK

    10. Eclipse Error : The Eclipse executable launcher was unable to locate its companion shared library.

    11. Add imports in eclipse shortcut key combinations

    12. How to check installed Eclipse version 32-bit or 64-bit Mac OS X

    13. How to Configure Eclipse keymap in IntelliJ IDE

    14. Eclipse Failed to load the JNI shared library jvm.dll

    15. Your System has Mac OS X Version 10.10. Java from Oracle requires 10.7.3 or later error

    16. appcompat_v7 errors after updates to API level 21 Material Theme

    17. Plug-in com.android.ide.eclipse.adt was unable to lead class Error

    18. The declared package does not match the expected package Eclipse

    19. incorrect line ending: found carriage return (\r) without corresponding newline (\n)

    20. Android : R Cannot Be Resolved To A Variable-Error Solutions

        more...






    Popular tags
    android
    x 175
    eclipse
    x 29
    notepadplusplus
    x 20
    macosx
    x 14
    sharepoint
    x 14
    html
    x 14
    mac
    x 13
    androidstudio
    x 11




    1000+ C Programs     PHP Tutorial     JSON Tutorial     Swift Tutorial     India Pinocdes     About Us     Privacy Policy


    Code2care © 2012-17