R cannot be resolved to a variable

后端 未结 29 1061
不知归路
不知归路 2020-11-28 14:14

I would like to fix this error

R cannot be resolved to a variable

I looked up many answers, but I could not get the right one;

相关标签:
29条回答
  • 2020-11-28 14:48
    • Rebuild
    • Clean ( Menu --> Project --> Clean )
    • Delete your import de.vogella.android.temprature1.R;
    0 讨论(0)
  • 2020-11-28 14:48

    Simple Solution:

    Check whether in gen[generated Java Files], under your project package, there is a R.java file.

    If no,

    1. There is some errors in xml files you have added or modified. check that deeply. Fix all yellow lints and go through the error log and console
    2. Check your xml files should be small letter, '_' and digits.
    3. Check your image names you have added also should be small letter, '_' and digits.
    4. Clean the project.
    5. Check R.java created. if yes, and still error occurs, follow the yes instructions below, else check xmls once again

    If yes,

    1. replace the 'import android.R' by your project package.R ex: 'com.mysmallapp.main.R' in all files.
    2. Clean the project
    0 讨论(0)
  • 2020-11-28 14:50

    If R is missing

    • Locate your R.java file, it should be in the folder ./gen/
    • If it is missing, and "build project" doesnt build it, one of your XML files has errors, fix them or even delete them, then right-click your project and click build project
    • R.java file should be generated
    0 讨论(0)
  • 2020-11-28 14:50

    I've seen many answers suggesting to "fix errors in your XML files" but only giving general hints and tips on how to fix the XML.

    Here is a systematic way to find the XML errors:

    Look at the "Problems" tab in Eclipse. Ignore all "R cannot be resolved to a variable". Look at what remains. For me, it looks something like this:

    Unparsed aapt error(s)! Check the console for output.
    

    I quote from http://developer.android.com/tools/building/index.html#detailed-build :

    The Android Asset Packaging Tool (aapt) takes your application resource files, such as the AndroidManifest.xml file and the XML files for your Activities, and compiles them. An R.java is also produced so you can reference your resources from your Java code.

    Now, open the "Console" tab. Window -> Show View > Console

    This should show you the exact error, in which file it occurred, and at what line number:

    [2014-05-13 08:27:05 - MyFirstApp] /Users/alain/code/experimental/android/adt_workspace4.4/MyFirstApp/res/menu/main_activity_actions.xml:4: error: Error: No resource found that matches the given name (at 'icon' with value '@drawable/ic_action_search').
    [2014-05-13 08:27:05 - MyFirstApp] /Users/alain/code/experimental/android/adt_workspace4.4/MyFirstApp/res/menu/main_activity_actions.xml:4: error: Error: No resource found that matches the given name (at 'title' with value '@string/action_search').
    

    In my case, the first error is because I am referencing ic_action_search but I have not yet put ic_action_search.png in my res/drawable-*dpi directories. The second error is because I am referencing a string "action_search" but I have not yet defined it in res/values/strings.xml

    It would have been nice if the ADT plugin actually took you to the line and file where the error is if you click on the error in the Console output, but for now, you have to do it manually.

    The problem with Eclipse is that is is not stopping the build when aapt fails but continues to the next stage of the build, which is the Java Compiler. The command-line tools do not have this problem:

    To generate build.xml:

    $ android list targets
    

    For me, I have:

    Available Android targets:
    ----------
    id: 1 or "android-16"
         Name: Android 4.1.2
         Type: Platform
         API level: 16
         Revision: 4
         Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, WXGA800-7in
     Tag/ABIs : default/armeabi-v7a, default/mips, default/x86
    ----------
    id: 2 or "android-19"
         Name: Android 4.4.2
         Type: Platform
         API level: 19
         Revision: 3
         Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, WXGA800-7in
     Tag/ABIs : default/armeabi-v7a
    

    Choose your target from the above list and use it in the next command:

    $ android update project --target <your_target_id> --path /path/to/android/project
    

    Now that build.xml is generated, you can build your project using one of these:

    $ ant debug
    $ ant -verbose debug
    $ ant -debug debug
    

    If the build fails at the aapt stage, it correctly does not generate R.java, but also it correctly does not continue to the Java Compiler stage. The console output will show you (just like in the Eclipse Console View) the filenames and line numbers where your XML errors are.

    and here is how to turn on line numbers in Eclipse:

    http://www.mkyong.com/eclipse/how-to-display-line-numbers-in-eclipse/

    Preferences -> General -> Editors -> Text Editors -> [x] Show line numbers
    
    0 讨论(0)
  • 2020-11-28 14:51

    for me, the best solution to suggest to people that have problems with the "R" file would be to try the next steps (order doesn't matter) :

    1. update ADT & SDK , Eclipse and Java (both 1.6 and the latest one).

      EDIT: now the ADT supports Java 1.7 . link here.

    2. remove gen folder , and create it again .

    3. do a clean-project.

    4. right click the project and choose android-tools -> fix-project-properties .

    5. right click the project and choose properties -> java-build-path -> order-and-export. make sure the order is :

      • Android 4.4 (always the latest version)

      • Android private libraries

      • android dependencies

      • your library project/s if needed

      • yourAppProject/gen

      • yourAppProject/src

    6. make sure all files in the res folder's subfolders have names that are ok : only lowercase letters, digits and underscore ("_") .

    7. always make sure the targetSdk is pointed to the latest API (currently 19) , and set it in the project.properties file

    8. try to run Lint and read its warnings. they might help you out.

    9. make sure your project compiles on 1.6 java and not a different version.

    10. restart eclipse.

    0 讨论(0)
  • 2020-11-28 14:52

    I do my projects in FlashBuilder 4.7 and it gerenerates a row in strings.xml:

    <string name="action_settings">Settings</string>

    Once i deleted it, i got the same errors. When returned it in place everythings worked again.

    0 讨论(0)
提交回复
热议问题