eclipse “R 无法解析为变量”- gen 文件夹为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9312267/
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 cannot be resolved to a variable' - gen folder is empty
提问by Quarra
I've got an error:
我有一个错误:
R cannot be resolved to a variable
in line:
排队:
setContentView(R.layout.main);
but I can't resolve it doing things that other users wrote should be tried, which are:
但我无法解决它做其他用户写的应该尝试的事情,它们是:
- Getting rid of 'import android.R' line.
- Cleaning the project and then building it again.
- Closing project and then opening it again.
- Going to Project Properties > Java Build Path > Tick Android Version Checkbox
- Following the procedure: Uncheck Project->Build Automatically Project->Properties->Builders: Make sure all Android (3) and Java (1) builders are checked (and nothing more) Window->Preferences->Android->Build: Check "Automatically refresh Resources and Assets folder on build" Right-click on project->Android Tools->Fix Project Settings (not sure what it actually does, but it can't hurt)
- Some users advice to check if .xml files are correct. I don't know how I could do that. I must admit I'm new to java and android programming, and I haven't even written a single line in my project - the code has been generated automaticly.
- Layout xml file is named using lower-case letters.
- No error in resource files (no red-crosses).
- 摆脱 'import android.R' 行。
- 清理项目,然后重新构建它。
- 关闭项目,然后再次打开它。
- 转到项目属性 > Java 构建路径 > 勾选 Android 版本复选框
- 执行以下步骤:取消选中 Project->Build Automatically Project->Properties->Builders:确保选中所有 Android (3) 和 Java (1) 构建器(仅此而已) Window->Preferences->Android->Build: Check “在构建时自动刷新资源和资产文件夹”右键单击项目->Android 工具->修复项目设置(不确定它实际上做了什么,但它不会受到伤害)
- 一些用户建议检查 .xml 文件是否正确。我不知道我怎么能做到这一点。我必须承认我是 java 和 android 编程的新手,我什至没有在我的项目中写过一行——代码是自动生成的。
- 布局 xml 文件使用小写字母命名。
- 资源文件中没有错误(没有红叉)。
layout main.xml file:
布局 main.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
Thanks in advance for your help!
在此先感谢您的帮助!
采纳答案by showlove
Yeah, i got stuck on this for hours. My solution was to: delete all projects from workspace->re-import the desired project by going to file->import->Browse->selecting the project->and finally check the "add project to working sets box" then clicking finish. The .R errors i once had then disappeared. I hope this helps someone!
是的,我坚持了几个小时。我的解决方案是:从工作区中删除所有项目->通过转到文件->导入->浏览->选择项目->最后选中“将项目添加到工作集框”然后单击完成来重新导入所需的项目. 我曾经的 .R 错误然后消失了。我希望这可以帮助别人!
回答by Blackbelt
Check your res/
folder in order to be sure that every file it contains has no error. If any error exists R
can not be generated.
检查您的res/
文件夹,以确保其中包含的每个文件都没有错误。如果存在任何错误,R
则无法生成。
回答by edthethird
Don't import Android.R;
别 import Android.R;
instead,
反而,
import package.name.R;
where package.name
is your package name declared in the manifest.
import package.name.R;
package.name
您在清单中声明的包名称在哪里。
Android.R
contains all the default layouts packaged with Android. Your custom layout will not be Android.R
, it will be in package.name.R;
Android.R
包含 Android 打包的所有默认布局。您的自定义布局不会Android.R
,它会在package.name.R;
回答by Quarra
Problem is solved, but I don't know what has changed... I reinstalled Eclipse, SDK etc. and genfolder was empty as before, the same problem.
问题解决了,但我不知道发生了什么变化......我重新安装了Eclipse,SDK等,并且gen文件夹和以前一样是空的,同样的问题。
My supervisor told me to do it all again, and it worked (for now). I don't know what was the cause. I though I'd had tried everything, but it's working now so I'll never know. Thanks for your help guys!
我的主管告诉我再做一遍,它奏效了(目前)。我不知道是什么原因。我虽然已经尝试了所有方法,但现在它正在起作用,所以我永远不会知道。谢谢你们的帮助!
回答by Dina
Nothing mentioned above worked for me. However, I fixed it myself.
上面提到的一切都不适合我。不过,我自己修好了。
Solution:
解决方案:
If you've any other files except activity_main.xml
/ main.xml
inside app_name/res/layout
remove it and try clean and build.
如果您有除activity_main.xml
/ main.xml
inside之外的任何其他文件,app_name/res/layout
请将其删除并尝试清理和构建。
Note:
笔记:
The unwanted files or (rather used later) files inside app_name/res/layout
would be *.out.xml
. Remove the same.
里面不需要的文件或(稍后使用)文件app_name/res/layout
将是*.out.xml
. 删除相同。
回答by jhon
Make sure that you are extending Activity class
确保您正在扩展 Activity 类
回答by Fofole
On eclipse you have the "Problems" window. If you fix all the other problems in there other than R cannot be resolved, the "R" problem will be auto-fixed as R is not generated because most likely you have project related problems.
在日食你有“问题”窗口。如果您修复了除 R 之外的所有其他问题无法解决,“R”问题将自动修复,因为 R 不会生成,因为很可能您遇到了与项目相关的问题。
Hope this helps. If you don't manage to do it, put a print of what your Problems window is showing.
希望这可以帮助。如果您无法做到,请打印“问题”窗口所显示的内容。
回答by Vikram Singh
Clean your project once. It should build that file again automatically.
Go to Project->Clean
Of course, it will work ony if your xml files are syntactically correct!
清理您的项目一次。它应该自动再次构建该文件。转到Project->Clean
当然,如果您的 xml 文件在语法上正确,它将起作用!
回答by Nitin Bansal
Delete your R.java fie and let it generate again...possibly this will help...i had same issue multiple times
删除你的 R.java fie 并让它再次生成......可能这会有所帮助......我多次遇到同样的问题
回答by AndroDev
Try this:
comment setContentView(R.layout.main);
and the build/clean your project again. Then uncomment the commented line. This issue should not be seen then.
试试这个:setContentView(R.layout.main);
再次评论和构建/清理你的项目。然后取消注释注释行。那时不应该看到这个问题。