eclipse 从现有源创建android项目后缺少R类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3232274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
R class missing after creating android project from existing source
提问by cstack
I downloaded the source for SpriteMethodTest, and I want to build it in Eclipse. So I Went:
我下载了 SpriteMethodTest 的源代码,我想在 Eclipse 中构建它。所以我去了:
File >> New >> Android Project >> Create Project From Existing Source >> SpriteMethodTest
文件>>新建>>Android项目>>从现有源创建项目>>SpriteMethodTest
It created the project alright, but the R class is not generated. Any file that references a resource in R says R cannot be resolved
.
它创建了项目,但没有生成 R 类。任何引用 R 中资源的文件都说R cannot be resolved
.
Importing android.R just results in R.drawable.background cannot be resolved
. How do I generate R again?
导入 android.R 只会导致R.drawable.background cannot be resolved
. 我如何再次生成 R?
回答by styler1972
You are having an issue because you are importing Androids R
, not your project specific R
. Simply delete the import android.R;
, then hover your mouse over the reference to the R that is giving the R cannot be resolved to a variable
. From there you will have possibly a few different options on which R to import. You want to import the R that is in yourpackage.
您遇到了问题,因为您要导入 Android R
,而不是特定于您的项目R
。只需删除import android.R;
,然后将鼠标悬停在提供R cannot be resolved to a variable
. 从那里你可能有几个不同的选项来导入 R。你想导入是在R的包。
回答by Sogger
The generated R resource file can only be generated by eclipse if the files it reads can compile, meaning errors in a file (such as the manifest file) prevent compilation. So, resolve your errors (listed in the 'Problems' tab) first, then save the changes and it should then be generated.
生成的 R 资源文件只能由 eclipse 生成,如果它读取的文件可以编译,这意味着文件(例如清单文件)中的错误会阻止编译。因此,首先解决您的错误(在“问题”选项卡中列出),然后保存更改,然后应该会生成它。
The reason it usually seems to be related to API versions is because different versions cause compatibility errors. This usually happens when people grab sample code and don't set the same API version as the code creator was using. Changing to the correct version resolves the errors and allows the R file to be generated.
通常看起来与API版本有关的原因是因为不同的版本会导致兼容性错误。当人们获取示例代码并且没有设置与代码创建者使用的相同的 API 版本时,通常会发生这种情况。更改为正确的版本可以解决错误并允许生成 R 文件。
回答by draq
- Create empty file inside "gen" folder and name it "R.java"
- click Project --> Clean...
- click Android Tools --> Fix Project...
- 在“gen”文件夹中创建空文件并将其命名为“R.java”
- 单击项目 --> 清理...
- 单击 Android 工具 --> 修复项目...
回答by Raphael Villas Boas
In my enviroment android SDK was missing the folder platform-tools and I had to start android and install platform-tools. Now it is working.
在我的环境中,android SDK 缺少文件夹平台工具,我不得不启动 android 并安装平台工具。现在它正在工作。
回答by Steve Blackwell
I had this exact same problem with SpriteMethodTest. It has nothing to do with package names or imports. As tarkeshwar says, if R is not being generated, then the problem is prior to compilation. In this case it's AndroidManifest.xml
.
我在SpriteMethodTest 上遇到了完全相同的问题。它与包名称或导入无关。正如 tarkeshwar 所说,如果未生成 R,则问题出在编译之前。在这种情况下,它是AndroidManifest.xml
.
The last tag in the manifest is
清单中的最后一个标签是
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="4"/>
but android:targetSdkVersion
is not a supported attribute. Change the line to
但android:targetSdkVersion
不是受支持的属性。将行更改为
<uses-sdk android:minSdkVersion="3"/>
and that should allow Eclipse to generate the R class.
这应该允许 Eclipse 生成 R 类。
回答by Nim
Had a similar problem, which seemed to be caused by the SDK versions as well:
有一个类似的问题,这似乎也是由 SDK 版本引起的:
- Project -->properties --> android: select the suitable API lvl and click OK. That caused the R class to finally be generated, how ever it will not compile yet.
- Delete the project and import it again (now it has the R class) and worked on first attempt.
- Project -->properties --> android:选择合适的API lvl,点击OK。这导致最终生成 R 类,但它尚未编译。
- 删除项目并再次导入它(现在它有 R 类)并进行了第一次尝试。
*Happened when tried to check out the snake example code.
*在尝试查看蛇示例代码时发生。
回答by msEmmaMays
Running this in the project folder fixed it for me
在项目文件夹中运行它为我修复了它
android update project -p .
I had "Build automatically" turned off when I created the new project, so certain files weren't created like they should have been
我在创建新项目时关闭了“自动构建”,因此某些文件没有像它们应该创建的那样创建
回答by tarkeshwar
R.java not being generated usually indicates problems elsewhere, for example: Manifest.xml having errors.
未生成 R.java 通常表示其他地方存在问题,例如:Manifest.xml 有错误。
Fix those errors and rebuild. R.java should get generated then.
修复这些错误并重建。然后应该生成 R.java。
If if it still not there, do project -> android -> fix android settings.
如果仍然不存在,请执行项目 -> android -> 修复 android 设置。
回答by Chris Thompson
Project -> Clean (select your project) -> Ok
项目 -> 清理(选择您的项目)-> 确定
This will trigger a re-build and so long as the project is configured as an Android project, R.java will be regenerated.
这将触发重新构建,只要将项目配置为 Android 项目,就会重新生成 R.java。
回答by Macarse
Try the following:
请尝试以下操作:
- Delete the gen folder
- Project => clean
- Right click => Android Tools => Fix project properties
- Right click => Run as... => Android application
- 删除gen文件夹
- 项目 => 干净
- 右键单击 => Android 工具 => 修复项目属性
- 右键单击 => 运行方式... => Android 应用程序