eclipse “R无法解析为变量”android错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14653329/
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" android error
提问by Ashni Goyal
I have freshly installed eclipse 3.8 and android sdk. When I create a new project I get error R cannot be resolved to a variable
in MainActivity.java
. R.java
is missing and instead I have BuildConfig.java
in gen folder. Based on suggestions given on similar questions I tried followings but no success.
我刚刚安装了 eclipse 3.8 和 android sdk。当我创建一个新项目时,我R cannot be resolved to a variable
在MainActivity.java
. R.java
丢失了,而是我BuildConfig.java
在 gen 文件夹中。根据对类似问题给出的建议,我尝试了以下但没有成功。
- Reinstalled ADT
- Cleaned the project
- Restart eclipse
- 重新安装 ADT
- 清理了项目
- 重启日食
Kindly help.
请帮助。
回答by Mike Marshall
Yes, XML files may be the culprit, but also make sure you have an import statement at the top of your file for it as well. Edithere is the typical beginning of a java file, you need the R import where my comment is
是的,XML 文件可能是罪魁祸首,但也要确保您的文件顶部也有一个导入语句。在这里编辑是一个java文件的典型开头,你需要我的评论所在的R导入
package your.package.name;
import your.package.name.R; // make sure you have this line
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
回答by Diogo Moreira
Verify the syntax on your XML files. The problem may not be in MainActivity.java. If there's an error on any of your XML files the class R won't be generated.
验证 XML 文件的语法。问题可能不在 MainActivity.java 中。如果您的任何 XML 文件出现错误,则不会生成类 R。
回答by Rocel
if you are using Eclipse, remove the R class import in your class and press
CTRL + SHIFT + O
, it will automatically import your classes.
If it proposes your move than one R class, select the one from your project.
如果您使用的是 Eclipse,请在您的类中删除 R 类导入并按
CTRL + SHIFT + O
,它将自动导入您的类。如果它建议您移动的不是一个 R 类,请从您的项目中选择一个。
回答by JoxTraex
If you have an error in your XML file then R.java will not be generated. Try cleaning your project and look for any signs of errors on your XML files.
如果您的 XML 文件中有错误,则不会生成 R.java。尝试清理您的项目并查找 XML 文件上的任何错误迹象。
回答by yogagunda
I had the same problem. The R.Java was not generated(in the gen folder). There was no error in the xml file. The issue was that I did not update the sdk completely. In the SDK manager we need to install the package whenever the "install package" button is enabled and close and open the sdk manager to check for updates. Close and reopen eclipse and check for updates in eclipse also.
我有同样的问题。未生成 R.Java(在 gen 文件夹中)。xml 文件中没有错误。问题是我没有完全更新 sdk。在 SDK 管理器中,我们需要在启用“安装包”按钮时安装包,并关闭并打开 sdk 管理器以检查更新。关闭并重新打开 eclipse 并检查 eclipse 中的更新。
回答by Alvaro
See this question Eclipse giving error, missing R.java file after recent update, probably help you if you recently installed the ADT
请参阅此问题Eclipse 给出错误,最近更新后丢失 R.java 文件,如果您最近安装了 ADT,可能会对您有所帮助