R不能解决——Android错误。

时间:2021-04-02 20:51:24

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

我刚刚下载并安装了新的Android SDK。我想创建一个简单的应用程序来测试驱动程序。

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. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

but Eclipse gives me the error

但是Eclipse给了我错误。

R cannot be resolved

R无法解决

on line

在网上

setContentView(R.layout.main);

Why?

为什么?

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

我有一个名为main的XML文件。在res / layout / xml。

105 个解决方案

#1


814  

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

在追踪这个问题之后,我在Android文档中发现了这条注释:

http://source.android.com/source/using-eclipse.html

http://source.android.com/source/using-eclipse.html

*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.*

*注意:Eclipse有时喜欢添加“导入android”。在您的文件顶部使用资源的语句,特别是当您要求Eclipse对导入进行排序或其他管理时。这将导致你的制造中断。注意这些错误的导入语句并删除它们。

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.

在浏览Android示例教程时,我经常使用Ctrl + Shift + O命令来“组织导入”,并生成任何缺失的导入语句。有时,这会生成不正确的import语句,从而隐藏R。构建时自动生成的java类。

#2


375  

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.

每次我遇到R的问题都没有生成,或者甚至消失,这是由于XML布局文件中的一些问题导致应用程序无法构建。

#3


188  

Whenever you get

当你得到

R cannot be resolved

R无法解决

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.

然后检查/res目录,其中一定有一些文件存在一些错误,这阻止了应用程序的构建。例如,它可能是一个布局文件,也可能是由于缺少资源,但是您已经在XML文件中定义了它。

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!

如果您有任何额外的,甚至未使用的(!)或未引用的(!)图像,在像res/drawables-mdpi这样的文件夹中,它不遵守文件命名约定(可能只包含[a-z0-9_.]), R。java类可能不会生成,导致事件链的所有其他文章都提到。希望它可以帮助!

#4


124  

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?

我的项目包括一个r.java。起初,出来。主要工作好。但是,在添加了一些代码之后,它就不起作用了,而错误就是r布局。主要不能解决。是什么问题?

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.

会去参加。如果是这样的话,删除它,这样您的项目将不会使用默认的Android资源类来解决R,而是使用您/res/文件夹中的自动生成的。

#5


93  

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.

我安装了新的ADT (v. 22)。它停止创建包含R.java的gen文件夹。解决方案是在Android SDK管理器中安装新的Android SDK构建工具。

Solution found here

解决方案在这里找到

#6


84  

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.

R。java是Android Eclipse插件在构建应用程序时创建的文件。R。java是在“gen”目录下创建的。该文件是由“res”目录中的信息生成的。如果您在Eclipse菜单上运行select“Project”->“Clean…”,它将删除并重新生成R。java文件。

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.

当您在AndroidManifest中更改包名称时,会发生“R无法解决”的问题。xml文件。它使用您的Android包名创建一个“gen”目录下的子目录,其中存储了R。java文件。

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.

Eclipse可能有执行clean的问题,因为它对R的位置感到困惑。java文件是当您更改了Android包的名称时。您可以重命名根目录下的子目录以匹配您的新包名,或者您可以将您的包名更改为旧名称。先清洗,然后将包名称更改为您想要的新名称。如果您在更改包名称的同时阻止Eclipse的构建,那么这是最有效的。在“项目”菜单下,取消选中“自动构建”的选项,并且在“Clean…”对话框中询问是否应该“立即启动构建”复选框,以便在更改包名称时不尝试构建。在您更改名称之后,您可以将“构建自动”返回。

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

注意,如果你的AndroidManifest。xml文件包名与您的Java包名不匹配,Eclipse将自动添加一个“导入 <您的android包名> . r;”在所有您的. Java文件中,如果您更改了您的AndroidManifest,就会有任何对r的引用。xml包名,有时Eclipse不更新所有这些添加的导入。如果出现这种情况,请使用Eclipse重构(ALT + Shift + R)将其中一个Java文件中的import语句更改为新的AndroidManifest。xml包名称。最好在禁用“自动构建”的时候这样做。

#7


82  

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.

R是一个自动生成的类,它持有用于标识>资源的常量。如果没有R。java文件(它将是gen/eu.mauriziopz.gps/R)。我建议您关闭并重新开放您的项目,或者去>项目>构建所有的项目(并根据>Josef的推荐,选择“自动构建”)。如果这个方法不奏效,那就试着做一个新项目,如果这个问题比>在这里重新创建,我们会更详细地讨论这个问题。

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.

但我发现还有另一个问题导致了第一个问题。SDK目录中的工具没有被执行的权限,所以它就像Eclipse中不存在的,因此它没有构建R。java文件。

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

因此修改权限和选择“构建自动”解决了问题。

#8


36  

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.

R是一个自动生成的类,它包含用于标识资源的常量。如果没有R。java文件(它将是gen/eu.mauriziopz.gps/R)。在Eclipse中使用1.5 SDK的java,我建议您关闭并重新打开您的项目,或者到>项目构建所有的项目(并根据Josef的建议选择“自动构建”)。如果这个方法不奏效,那就试着做一个新项目,如果这个问题被重新创建,而不是在这里重新发布,我们将会更详细地讨论。

#9


28  

Close all files, clean project, restart Eclipse.

关闭所有文件,清理项目,重新启动Eclipse。

#10


19  

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.

com . example。例如,如果myapp是您的包的根命名空间,您可以将一个活动添加到com.example.myapp中。活动名称空间。

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

这将产生“R不能被解决”的错误。

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

要修复活动中的默认名称空间中的R,应该是:

import com.example.myapp.R;

#11


19  

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

在前面的答案中,你要确定你的Android目标是:

  1. Right-click on your project
  2. 右键单击您的项目
  3. Choose Properties
  4. 选择属性
  5. Choose Android in the left menu
  6. 在左侧菜单中选择Android。
  7. Tick a box next to the appropriate Project Build Target.
  8. 在适当的项目构建目标旁边打勾。
  9. Click Apply and OK
  10. 单击Apply和好的

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.

一年后,我找到了另一个原因。我的drawable文件夹中有一个.jpg图像,其名称与.png图像相同。在我的代码中引用这个图像一定会使程序混乱,它给了“R不能被解决”的错误。

#12


19  

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

在AndroidManifest.xml中检查是值得的。属性包具有正确的值。

That is:

那就是:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="your.correct.package.name"
   ...

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

改变之后,R。java将重新生成。

#13


16  

Make sure you installed the Android build tool form sdk manager

确保您安装了Android构建工具form sdk管理器。

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

项目右击属性-> Java BuildPath选择库并添加android支持。把下面这些步骤按一下。

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

转到Project->属性->Java构建路径,而不是选择Order和export选项卡。设置android-support .jar库,并将其添加到列表的顶部。和清洁和重建。它适用于大多数情况。

R不能解决——Android错误。

#14


14  

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\\._].

我第一次遇到这个问题,并意识到是什么原因造成的:我用大写字母创建了一个XML文件。在/res中所有的XML文件名必须匹配[a-z0-9\ ._]。

#15


13  

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

最简单的解决方案—有时您只需保存正在处理的XML文件,以使自动生成器启动。

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.

保存文件(例如main.xml)然后删除R。java文件,看看是否重新生成R。java解决了R解决问题。

#16


11  

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

检查XML文件名。确保它们都是小写的。

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.

还要确保任何图像资源名都是小写的。我在我的jpg文件的名称中有一个大写字母,它在我的项目中造成了R未解决的错误。

#17


11  

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.

R是一个生成的类。如果您使用的是Android开发工具(ADT),它是在项目构建时生成的。你可能会“自动构建”关闭。

#18


10  

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.

这个错误出现在我的x64 Linux Mint安装上。结果是ADB二进制文件的结果是失败的,因为没有安装ia32-libs包。简单地运行apt-get安装ia32-libs并重新启动Eclipse修复错误。

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

如果您的x64发行版没有ia32-libs,您将不得不使用Multiarch。

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

查看这篇文章的第4和第5条:http://crunchbang.org/forums/viewtopic.php?

Hope this helps someone.

希望这可以帮助别人。

#19


9  

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

您可能需要更新SDK工具。重新启动Android SDK管理器并安装一个新项目:Android SDK构建工具。

R不能解决——Android错误。

#20


7  

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.

我也有这个问题。结果是我无意中从字符串中删除了“app_name”字符串资源。xml文件,导致了一个无声的错误。一旦我添加了它,R类就会成功地生成,并且所有的东西都备份和运行。

#21


7  

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.R不能解决——Android错误。

您可能需要更新/安装SDK工具。重新启动Android SDK管理器并安装一个新项目:Android SDK构建工具。一个一个删除,修正一个为你工作。

#22


5  

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

尝试让新的XML布局文件命名为小写。例如,使用my_file。xml而不是myFile.xml。

#23


5  

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.

另一个原因。如果您有像res/drawable-hdpi、res/drawable-mdpi或res/drawable-ldpi之类的目录,那么java可能不会自动生成。

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.

1.6+似乎可以使用这些目录,但1.5不需要它们。当我移除这些目录时,R。java又开始为我自动生成。

#24


5  

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:

不幸的是,现有的问题都没有得到帮助。因为我的问题似乎是针对特定平台的,所以这可能只适用于你在Ubuntu 64位上(我目前正在使用Ubuntu 12)。起初,我没有看到“问题”窗口,它已经暗示了一个解决方案,在64位环境中,您需要32位的libs,这可能会丢失:

sudo apt-get install ia32-libs

sudo apt-get安装ia32-libs

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

我安装了这个包并重新构建了我的项目,这个项目为我解决了这个问题。

#25


5  

restart your computer.
Nothing helped me except this way

重启你的电脑。除了这一点,没有什么能帮助我。

#26


5  

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

只要去Android上的菜单列表就可以了。单击“构建”菜单,在“构建”下单击“重建项目”。

R不能解决——Android错误。

#27


4  

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.

我有Android 8的例子,正在尝试使用Android 7 SDK。当我关闭项目并重新打开应用程序文件夹并选择使用Android 8 SDK时,它能够找到R文件。希望这个有帮助。

#28


4  

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.

在我的例子中,我在AndroidManifest.xml中有一个错误。其他人说,您的XML文件必须没有错误,但我只是查看res/文件夹。找出并修复尽可能多的错误,这个问题可能会自行解决。

#29


4  

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

通常情况下,这是因为创建项目时提供的MinSDK版本号。例子:

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

如果你想让2.1成为最小值,那么Android 2.1实际上就是API级别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.

当你浏览你下载安装的SDK时,你可以看到我在说什么。导航到您将SDK安装到的地方(例如,C:\android-sdk-windows),并打开名为“平台”的文件夹。你会看到类似“android-7”的文件夹,如果你打开它,就会有一个来源。当使用文本编辑器打开时,属性文件将显示相应的平台版本。

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.

当您创建一个项目时,您必须选择一个“构建目标”API,该列表中最后一个名为“API级别”的列显示了在填充MinSDK设置时要查找的数字。

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.

这可能是导致R的最常见的错误之一。java文件不是在> gen > packagename > .java项目下创建的。

#30


4  

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.

删除main.out.xml。我是新手,还不知道这个文件是用来干什么的,但是删除它清除了问题。

#1


814  

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

在追踪这个问题之后,我在Android文档中发现了这条注释:

http://source.android.com/source/using-eclipse.html

http://source.android.com/source/using-eclipse.html

*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.*

*注意:Eclipse有时喜欢添加“导入android”。在您的文件顶部使用资源的语句,特别是当您要求Eclipse对导入进行排序或其他管理时。这将导致你的制造中断。注意这些错误的导入语句并删除它们。

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.

在浏览Android示例教程时,我经常使用Ctrl + Shift + O命令来“组织导入”,并生成任何缺失的导入语句。有时,这会生成不正确的import语句,从而隐藏R。构建时自动生成的java类。

#2


375  

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.

每次我遇到R的问题都没有生成,或者甚至消失,这是由于XML布局文件中的一些问题导致应用程序无法构建。

#3


188  

Whenever you get

当你得到

R cannot be resolved

R无法解决

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.

然后检查/res目录,其中一定有一些文件存在一些错误,这阻止了应用程序的构建。例如,它可能是一个布局文件,也可能是由于缺少资源,但是您已经在XML文件中定义了它。

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!

如果您有任何额外的,甚至未使用的(!)或未引用的(!)图像,在像res/drawables-mdpi这样的文件夹中,它不遵守文件命名约定(可能只包含[a-z0-9_.]), R。java类可能不会生成,导致事件链的所有其他文章都提到。希望它可以帮助!

#4


124  

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?

我的项目包括一个r.java。起初,出来。主要工作好。但是,在添加了一些代码之后,它就不起作用了,而错误就是r布局。主要不能解决。是什么问题?

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.

会去参加。如果是这样的话,删除它,这样您的项目将不会使用默认的Android资源类来解决R,而是使用您/res/文件夹中的自动生成的。

#5


93  

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.

我安装了新的ADT (v. 22)。它停止创建包含R.java的gen文件夹。解决方案是在Android SDK管理器中安装新的Android SDK构建工具。

Solution found here

解决方案在这里找到

#6


84  

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.

R。java是Android Eclipse插件在构建应用程序时创建的文件。R。java是在“gen”目录下创建的。该文件是由“res”目录中的信息生成的。如果您在Eclipse菜单上运行select“Project”->“Clean…”,它将删除并重新生成R。java文件。

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.

当您在AndroidManifest中更改包名称时,会发生“R无法解决”的问题。xml文件。它使用您的Android包名创建一个“gen”目录下的子目录,其中存储了R。java文件。

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.

Eclipse可能有执行clean的问题,因为它对R的位置感到困惑。java文件是当您更改了Android包的名称时。您可以重命名根目录下的子目录以匹配您的新包名,或者您可以将您的包名更改为旧名称。先清洗,然后将包名称更改为您想要的新名称。如果您在更改包名称的同时阻止Eclipse的构建,那么这是最有效的。在“项目”菜单下,取消选中“自动构建”的选项,并且在“Clean…”对话框中询问是否应该“立即启动构建”复选框,以便在更改包名称时不尝试构建。在您更改名称之后,您可以将“构建自动”返回。

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

注意,如果你的AndroidManifest。xml文件包名与您的Java包名不匹配,Eclipse将自动添加一个“导入 <您的android包名> . r;”在所有您的. Java文件中,如果您更改了您的AndroidManifest,就会有任何对r的引用。xml包名,有时Eclipse不更新所有这些添加的导入。如果出现这种情况,请使用Eclipse重构(ALT + Shift + R)将其中一个Java文件中的import语句更改为新的AndroidManifest。xml包名称。最好在禁用“自动构建”的时候这样做。

#7


82  

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.

R是一个自动生成的类,它持有用于标识>资源的常量。如果没有R。java文件(它将是gen/eu.mauriziopz.gps/R)。我建议您关闭并重新开放您的项目,或者去>项目>构建所有的项目(并根据>Josef的推荐,选择“自动构建”)。如果这个方法不奏效,那就试着做一个新项目,如果这个问题比>在这里重新创建,我们会更详细地讨论这个问题。

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.

但我发现还有另一个问题导致了第一个问题。SDK目录中的工具没有被执行的权限,所以它就像Eclipse中不存在的,因此它没有构建R。java文件。

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

因此修改权限和选择“构建自动”解决了问题。

#8


36  

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.

R是一个自动生成的类,它包含用于标识资源的常量。如果没有R。java文件(它将是gen/eu.mauriziopz.gps/R)。在Eclipse中使用1.5 SDK的java,我建议您关闭并重新打开您的项目,或者到>项目构建所有的项目(并根据Josef的建议选择“自动构建”)。如果这个方法不奏效,那就试着做一个新项目,如果这个问题被重新创建,而不是在这里重新发布,我们将会更详细地讨论。

#9


28  

Close all files, clean project, restart Eclipse.

关闭所有文件,清理项目,重新启动Eclipse。

#10


19  

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.

com . example。例如,如果myapp是您的包的根命名空间,您可以将一个活动添加到com.example.myapp中。活动名称空间。

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

这将产生“R不能被解决”的错误。

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

要修复活动中的默认名称空间中的R,应该是:

import com.example.myapp.R;

#11


19  

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

在前面的答案中,你要确定你的Android目标是:

  1. Right-click on your project
  2. 右键单击您的项目
  3. Choose Properties
  4. 选择属性
  5. Choose Android in the left menu
  6. 在左侧菜单中选择Android。
  7. Tick a box next to the appropriate Project Build Target.
  8. 在适当的项目构建目标旁边打勾。
  9. Click Apply and OK
  10. 单击Apply和好的

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.

一年后,我找到了另一个原因。我的drawable文件夹中有一个.jpg图像,其名称与.png图像相同。在我的代码中引用这个图像一定会使程序混乱,它给了“R不能被解决”的错误。

#12


19  

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

在AndroidManifest.xml中检查是值得的。属性包具有正确的值。

That is:

那就是:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="your.correct.package.name"
   ...

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

改变之后,R。java将重新生成。

#13


16  

Make sure you installed the Android build tool form sdk manager

确保您安装了Android构建工具form sdk管理器。

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

项目右击属性-> Java BuildPath选择库并添加android支持。把下面这些步骤按一下。

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

转到Project->属性->Java构建路径,而不是选择Order和export选项卡。设置android-support .jar库,并将其添加到列表的顶部。和清洁和重建。它适用于大多数情况。

R不能解决——Android错误。

#14


14  

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\\._].

我第一次遇到这个问题,并意识到是什么原因造成的:我用大写字母创建了一个XML文件。在/res中所有的XML文件名必须匹配[a-z0-9\ ._]。

#15


13  

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

最简单的解决方案—有时您只需保存正在处理的XML文件,以使自动生成器启动。

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.

保存文件(例如main.xml)然后删除R。java文件,看看是否重新生成R。java解决了R解决问题。

#16


11  

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

检查XML文件名。确保它们都是小写的。

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.

还要确保任何图像资源名都是小写的。我在我的jpg文件的名称中有一个大写字母,它在我的项目中造成了R未解决的错误。

#17


11  

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.

R是一个生成的类。如果您使用的是Android开发工具(ADT),它是在项目构建时生成的。你可能会“自动构建”关闭。

#18


10  

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.

这个错误出现在我的x64 Linux Mint安装上。结果是ADB二进制文件的结果是失败的,因为没有安装ia32-libs包。简单地运行apt-get安装ia32-libs并重新启动Eclipse修复错误。

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

如果您的x64发行版没有ia32-libs,您将不得不使用Multiarch。

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

查看这篇文章的第4和第5条:http://crunchbang.org/forums/viewtopic.php?

Hope this helps someone.

希望这可以帮助别人。

#19


9  

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

您可能需要更新SDK工具。重新启动Android SDK管理器并安装一个新项目:Android SDK构建工具。

R不能解决——Android错误。

#20


7  

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.

我也有这个问题。结果是我无意中从字符串中删除了“app_name”字符串资源。xml文件,导致了一个无声的错误。一旦我添加了它,R类就会成功地生成,并且所有的东西都备份和运行。

#21


7  

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.R不能解决——Android错误。

您可能需要更新/安装SDK工具。重新启动Android SDK管理器并安装一个新项目:Android SDK构建工具。一个一个删除,修正一个为你工作。

#22


5  

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

尝试让新的XML布局文件命名为小写。例如,使用my_file。xml而不是myFile.xml。

#23


5  

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.

另一个原因。如果您有像res/drawable-hdpi、res/drawable-mdpi或res/drawable-ldpi之类的目录,那么java可能不会自动生成。

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.

1.6+似乎可以使用这些目录,但1.5不需要它们。当我移除这些目录时,R。java又开始为我自动生成。

#24


5  

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:

不幸的是,现有的问题都没有得到帮助。因为我的问题似乎是针对特定平台的,所以这可能只适用于你在Ubuntu 64位上(我目前正在使用Ubuntu 12)。起初,我没有看到“问题”窗口,它已经暗示了一个解决方案,在64位环境中,您需要32位的libs,这可能会丢失:

sudo apt-get install ia32-libs

sudo apt-get安装ia32-libs

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

我安装了这个包并重新构建了我的项目,这个项目为我解决了这个问题。

#25


5  

restart your computer.
Nothing helped me except this way

重启你的电脑。除了这一点,没有什么能帮助我。

#26


5  

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

只要去Android上的菜单列表就可以了。单击“构建”菜单,在“构建”下单击“重建项目”。

R不能解决——Android错误。

#27


4  

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.

我有Android 8的例子,正在尝试使用Android 7 SDK。当我关闭项目并重新打开应用程序文件夹并选择使用Android 8 SDK时,它能够找到R文件。希望这个有帮助。

#28


4  

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.

在我的例子中,我在AndroidManifest.xml中有一个错误。其他人说,您的XML文件必须没有错误,但我只是查看res/文件夹。找出并修复尽可能多的错误,这个问题可能会自行解决。

#29


4  

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

通常情况下,这是因为创建项目时提供的MinSDK版本号。例子:

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

如果你想让2.1成为最小值,那么Android 2.1实际上就是API级别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.

当你浏览你下载安装的SDK时,你可以看到我在说什么。导航到您将SDK安装到的地方(例如,C:\android-sdk-windows),并打开名为“平台”的文件夹。你会看到类似“android-7”的文件夹,如果你打开它,就会有一个来源。当使用文本编辑器打开时,属性文件将显示相应的平台版本。

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.

当您创建一个项目时,您必须选择一个“构建目标”API,该列表中最后一个名为“API级别”的列显示了在填充MinSDK设置时要查找的数字。

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.

这可能是导致R的最常见的错误之一。java文件不是在> gen > packagename > .java项目下创建的。

#30


4  

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.

删除main.out.xml。我是新手,还不知道这个文件是用来干什么的,但是删除它清除了问题。