2009-05-19 21:02
采纳率: 50%
浏览 424

R 不能被解决-Android 错误

I just downloaded and installed the new Android SDK. I wanted to create a simple application to test drive it.

The wizard created this code:

package eu.mauriziopz.gps;

import android.app.Activity;
import android.os.Bundle;

public class ggps extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

but Eclipse gives me the error

R cannot be resolved

on line



PS: I do have an XML file named main.xml under res/layout/.


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

30条回答 默认 最新

  • 斗士狗 2011-09-22 11:22

    What Will said was right

    R is an automatically generated class that holds the constants used to identify your >resources. If you don't have an R.java file (it would be gen/eu.mauriziopz.gps/R.java in >Eclipse with the 1.5 SDK) I would recommend closing and reopening your project or going to >Project > Build all (and selecting "Build Automatically" while there as recommended by >Josef). If that doesn't work than try making a new project, if the problem is recreated than >post here again and we'll go into more detail.

    but I've found out that there was another problem that was causing the first one. The tools in the SDK directory didn't have the permissions to be executed, so it was like the didn't exist for Eclipse, thus it didn't build the R.java file.

    So modifying the permission and selecting "Build Automatically" solved the problem.

    打赏 评论
  • ℡Wang Yan 2011-09-22 11:22

    This error cropped up on my x64 Linux Mint installation. It turned out that the result was a failure in the ADB binary, because the ia32-libs package was not installed. Simply running apt-get install ia32-libs and relaunching Eclipse fixed the error.

    If your x64 distro does not have ia32-libs, you'll have to go Multiarch.

    Check #4 and #5 on this post: http://crunchbang.org/forums/viewtopic.php?pid=277883#p277883

    Hope this helps someone.

    打赏 评论
  • perhaps? 2011-09-22 11:22

    It is worth checking in AndroidManifest.xml. The attribute package has the correct value.

    That is:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"

    After you change that, the R.java will be re-generated.

    打赏 评论
  • 斗士狗 2011-09-22 11:22

    Simplest solution - Sometimes you just need to save the XML file you were working on to get the autogenerator to kick in.

    Save the file (e.g. main.xml) then delete the R.java file and see if the regenerated R.java resolves the R resolve problem.

    打赏 评论
  • derek5. 2011-09-22 11:22

    Check the XML file names. Be sure that they're all in lowercase.

    Also make sure that any image resource names are also all in LOWER CASE. I had a capital letter in the name of my jpg file, and it caused the R unresolved error right across my project.

    打赏 评论
  • 喵-见缝插针 2011-09-22 11:22

    Yet another reason R.java might not get autogenerated is if you have directories like res/drawable-hdpi, res/drawable-mdpi, or res/drawable-ldpi.

    1.6+ seems to be OK with these directories, but 1.5 doesn't want them. When I removed those directories, R.java started autogenerating for me again.

    打赏 评论
  • 喵-见缝插针 2011-09-22 11:22

    I had this problem as well. It turned out that I had inadvertently deleted the "app_name" string resource from the strings.xml file, which was causing a silent error. Once I added it back, the R class was generated successfully and everything was back up and running.

    打赏 评论
  • larry*wei 2011-09-22 11:22

    I just had this problem for the millionth time and realized what was causing it: I created an XML file with uppercase letters in the name. All your XML filenames in /res must match [a-z0-9\\._].

    打赏 评论
  • 零零乙 2011-09-22 11:22

    R is an automatically generated class that holds the constants used to identify your resources. If you don't have an R.java file (it would be gen/eu.mauriziopz.gps/R.java in Eclipse with the 1.5 SDK) I would recommend closing and reopening your project or going to Project > Build all (and selecting "Build Automatically" while there as recommended by Josef). If that doesn't work than try making a new project, if the problem is recreated than post here again and we'll go into more detail.

    打赏 评论
  • 乱世@小熊 2011-09-22 11:22

    Remove main.out.xml. I'm new to this and don't yet know what this file is used for, but removing it cleared the problem.

    打赏 评论
  • 叼花硬汉 2011-09-22 11:22

    Try to make your new XML layout file name lower case. For example, use my_file.xml instead of myFile.xml.

    打赏 评论
  • 零零乙 2011-09-22 11:22

    R.java is a file that the Android Eclipse plugins creates while building your application. R.java is created under the "gen" directory. This file is generated from the information in the "res" directory. If you run select "Project" -> "Clean..." on the Eclipse menu, it will remove and then regenerate the R.java file.

    The problem "R cannot be resolved" happens when you change your package name in the AndroidManifest.xml file. It uses your Android package name to create a subdirectory under the "gen" directory where it stores the R.java file.

    Eclipse may have problems executing clean, because it is confused about where the R.java file is when you have changed the Android package name. You can either rename the subdirectory under gen to match your new package name, or you can change your package name back to the old name. Do the clean and then change the package name to the new name you want. This works best if you stop Eclipse from trying to build while you are changing the package name. Under the "Project" menu uncheck the option to "Build Automatically" and also when the "Clean..." dialog asks if it should "Start a build immediately" uncheck the box so it doesn't try to build while you are changing the package name. After you have changed the name you can turn "Build Automatically" back on again.

    Note that if your AndroidManifest.xml file package name does not match your Java package name, Eclipse will end up automatically adding an "import <your Android package name>.R;" line in all your .java files that have any references to R. If you change your AndroidManifest.xml package name, sometimes Eclipse does not update all of these added imports. If that happens, use the Eclipse refactoring (ALT + Shift + R) to change the import statement in one of your Java files to your new AndroidManifest.xml package name. It is best to do this while you have disabled "Build Automatically".

    打赏 评论
  • 7*4 2011-09-22 11:22

    This error can also be caused by adding an activity to a namespace that is different to the root namespace for your package.

    For example, if com.example.myapp is the root namespace for your package, you can then add an activity to the com.example.myapp.activities namespace.

    This will produce the "R cannot be resolved" error.

    To fix the import the R in the default namespace in your activity should be:

    import com.example.myapp.R;
    打赏 评论
  • ~Onlooker 2011-09-22 11:22

    R is a generated class. If you are using the Android Development Tools (ADT) it is generated whenever the project is built. You may have 'Build Automatically' turned off.

    打赏 评论
  • 谁还没个明天 2011-09-22 11:22

    Close all files, clean project, restart Eclipse.

    打赏 评论
  • Didn"t forge 2011-09-22 11:22

    I had the examples of Android 8 and was trying to use Android 7 SDK. When I closed the project and reopened the application folder and chose to use Android 8 SDK, it was able to find the R file. Hope this helps.

    打赏 评论
  • 叼花硬汉 2011-09-22 11:22

    Often times this is because of the MinSDK version number you supplied when creating the project. Example:

    If you want 2.1 to be the minimum, Android 2.1 is actually API Level 7.

    You can see what I am talking about when you browse the SDK you downloaded and installed. Navigate to the place you installed the SDK to (C:\android-sdk-windows for example) and open the folder named "platforms". You will see something like "android-7" listed as a folder, and if you open that there is a source.properties file that, when opened with a text editor, will show you the corresponding platform version.

    When you create a project, and you must select a "Build Target" API, the last column in that list named "API Level" shows the number you are looking for when populating the MinSDK setting.

    This is probably one of the most common mistakes that results in the R.java file not being created under Project > gen > packagename > R.java.

    打赏 评论
  • 叼花硬汉 2011-09-22 11:22

    After tracking down this problem as well, I found this note in the Android documentation:


    *Note: Eclipse sometimes likes to add an "import android.R" statement at the top of your files that use resources, especially when you ask Eclipse to sort or otherwise manage imports. This will cause your make to break. Look out for these erroneous import statements and delete them.*

    While going through the Android sample tutorials, I would often use the Ctrl + Shift + O command to "Organize Imports" and generate any missing import statements. Sometimes this would generate the incorrect import statement which would hide the R.java class that is automatically generated when you build.

    打赏 评论
  • bug^君 2011-09-22 11:22

    my project have include a r.java.at the beginning ,R.layout.main work good.But,after adding some code it doesn't work,and the error is R.layout.main can't resolved.what's the problem?

    Look at your imports. Chances are that the line:

    import android.R;

    will be there. If that's the case, remove it, so that your project will resolve R not with the default Android Resources class, but with the one auto-generated from your /res/ folder.

    打赏 评论
  • csdnceshi62 2011-09-22 11:22

    Each time I had a problem with R not been generated, or even disappeared, this was due to some problem in the XML layout file that prevented the application from being built.

    打赏 评论
  • 笑故挽风 2011-09-22 11:22

    Whenever you get

    R cannot be resolved

    then check for the /res directory and there must be some file that have some error in it and that is preventing the application from being built. For example, it may be a layout file or it may be due to some missing resource is, but you already defined it in the XML file.

    If you have any additional, even unused (!) or unreferenced (!) images in a folder like res/drawables-mdpi which do not comply to the file naming conventions (may contain only [a-z0-9_.]), the R.java class might not generate, causing the chain of events all the other posts referred to. Hope it helps!

    打赏 评论
  • elliott.david 2011-12-21 17:09

    In my case, I had an error in my AndroidManifest.xml. Others have said that your XML files must be free from errors, but I was only looking in the res/ folder. Find and fix as many possible errors and the problem may well resolve itself.

    打赏 评论
  • 北城已荒凉 2013-02-15 09:32

    Along with the great suggestions in the previous answers, make sure your Android target is set:

    1. Right-click on your project
    2. Choose Properties
    3. Choose Android in the left menu
    4. Tick a box next to the appropriate Project Build Target.
    5. Click Apply and OK

    Edit: A year later I found another cause. I had a .jpg image in my drawable folder with the same name as a .png image. Referencing this image in my code must have confused the program and it gave the "R cannot be resolved" error.

    打赏 评论
  • from.. 2013-02-23 10:37

    Unfortunately none of the existing questions helped. Since my problem seems platform specific, this may only work if you are on Ubuntu 64 Bit (I am currently using Ubuntu 12). At first I did not see the "Problems" Window, which already hinted a solution, that on 64 Bit Environment you need the 32 Bit libs, which may be missing:

    sudo apt-get install ia32-libs

    I installed the package and rebuild my project, which fixed the issue for me.

    打赏 评论
  • 衫裤跑路 2013-05-17 12:28

    And another thing which may cause this problem:

    I installed the new ADT (v. 22). It stopped creating gen folder which includes R.java. The solution was to also install new Android SDK Build Tools from Android SDK Manager.

    Solution found here

    打赏 评论
  • 撒拉嘿哟木头 2013-08-29 07:09

    Make sure you installed the Android build tool form sdk manager

    project right click properties-> Java BuildPath select Library and add android-support.jar the follow these step.

    Go to Project->Properties->Java Build Path than select Order and export tab. Set android-support .jar library checked and up it into top of the list. And clean and rebuild..It works for most of the cases

    enter image description here

    打赏 评论
  • ~Onlooker 2013-11-06 04:19

    You may need to update SDK tools. Relaunch Android SDK Manager again and install a new item: Android SDK Build-tools.

    enter image description here

    打赏 评论
  • 妄徒之命 2016-03-29 06:28

    Just go to Android Top menu list. click on Build Menu, in under Build click on Rebuild Project.

    enter image description here

    打赏 评论
  • 乱世@小熊 2016-04-02 01:36

    You may need to update/install SDK tools. Relaunch Android SDK Manager again and install a new item: Android SDK Build-tools.one by one delete,fix which one work for you.enter image description here

    打赏 评论
  • First check is there any error in any xml layout or not, if then resolve it first.

    Otherwise remove junit dependency from project and rebuild the project.

    enter image description here

    打赏 评论