R 无法解析为变量 - Eclipse 查找错误的技巧?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15115092/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:04:39  来源:igfitidea点击:

R cannot be resolved to a variable - Eclipse trick to find the error?

androideclipse

提问by WhiskThimble

I have an issue that many people had, looking at the numerous questions, which is described in the subject : sometimes, R.java disappears and after some (far too long) minutes of changing back what I did, R.java comes back. When R.java isn't generated, lines calling for R show the error : 'R cannot be resolved to a variable'.

在查看主题中描述的众多问题时,我遇到了许多人遇到的问题:有时,R.java 消失了,经过一些(太长)分钟的更改后,R.java 又回来了。当未生成 R.java 时,调用 R 的行显示错误:“R 无法解析为变量”。

I know that R isn't the real problem, and that there is one or more error somewhere in my code.

我知道 R 并不是真正的问题,而且我的代码中某处存在一个或多个错误。

My question is : how to localize these errors, provided Eclipse doesn't show them?

我的问题是:如果 Eclipse 不显示这些错误,如何定位这些错误?

If you have the same problem as me, first see "R cannot be resolved to a variable"?which can help you getting rid of that mercyless error

如果你和我有同样的问题,先看“R无法解析为变量”?这可以帮助你摆脱那个无情的错误

回答by Raghav Sood

If you're trying to pinpoint the source of this error, try this:

如果您想查明此错误的来源,请尝试以下操作:

  1. Go to Project -> Clean. This will ensure that your error wasn't because of a bad build. If R can still not be found, move onto step 2.
  2. You should have a Problemstab in Eclipse which will list out all the errors and warnings in open projects, as shown in the screenshot below. If you can't see the Problems tab, go to Window -> Show View -> Problems
  3. Expand the Errorssection to see what problems are present. You will be told the problem, the project, the file and the line number in most cases.
  1. Project -> Clean。这将确保您的错误不是因为错误的构建。如果仍然找不到 R,则转到第 2 步。
  2. 您应该Problems在 Eclipse 中有一个选项卡,它将列出打开项目中的所有错误和警告,如下面的屏幕截图所示。如果您看不到问题选项卡,请转到Window -> Show View -> Problems
  3. 展开该Errors部分以查看存在哪些问题。在大多数情况下,您会被告知问题、项目、文件和行号。

enter image description here

在此处输入图片说明

回答by CommonsWare

how to localize these errors, provided Eclipse doesn't show them?

如果 Eclipse 不显示这些错误,如何定位这些错误?

Usually, Eclipse doesshow them. The errors will be in a resource or your manifest, usually indicated by a red X badge over the icon of the file in the Package Manager. Usually, the Console will contain a log message about the error as well.

通常,Eclipse显示它们。错误将在资源或您的清单中,通常由包管理器中文件图标上方的红色 X 标记指示。通常,控制台也会包含有关错误的日志消息。

Push come to shove, you can do a command-line build and see what that reports.

推来推去,您可以进行命令行构建并查看报告的内容。

回答by VinceFR

Eclipse won't build the R.javafile if there's somethin wrong in your /resfolder or other XML files.

R.java如果您的/res文件夹或其他 XML 文件中存在错误,Eclipse 将不会构建该文件。

I recommend you to check that folder.

我建议您检查该文件夹。

回答by Fred F

If clean does not work and there is no error other than missing R.java restart the eclipse.

如果 clean 不起作用并且除了缺少 R.java 之外没有其他错误,请重新启动 eclipse。

File Menu->restart

File Menu->restart

回答by WhiskThimble

Found the issue, but can't figure out how to find it simply. I accenditally erased this line < string name="action_settings">Settings< /string>while doing copy past of another one.

找到了问题,但无法弄清楚如何简单地找到它。我< string name="action_settings">Settings< /string>在复制另一行时偶然删除了这一行。

Don't know what it is used for, but Eclipse didn't show this in error log, nor anywhere I looked for.

不知道它是做什么用的,但是 Eclipse 没有在错误日志中显示这个,也没有在我寻找的任何地方显示。

Thanks anyway guys for the help, hope I won't have some similar problem again!

还是谢谢大家的帮助,希望以后不要再遇到类似的问题!

回答by eTech Ans

1) see if the xml files in the Package Explorer tree have an error-icon if they do, fix errors and R-class will be generated.

1)查看Package Explorer树中的xml文件是否有错误图标,如果有,修复错误并生成R-class。

example hint: FILL_PARENT was renamed MATCH_PARENT in API Level 8; so if you're targeting platform 2.1 (api 7), change all occurences back to "fill_parent"

示例提示:在 API 级别 8 中,FILL_PARENT 被重命名为 MATCH_PARENT;因此,如果您的目标是平台 2.1 (api 7),请将所有出现的事件改回“fill_parent”

2) if you added "import android.R;" trying to do a quick-fix, remove that line

2) 如果你添加了“import android.R;” 尝试快速修复,删除该行

3) Check your AndroidManifest.xml file, see if the package name is correct. If you copied an example it is likely this is wrong and the source of your problem

3)检查你的AndroidManifest.xml文件,看包名是否正确。如果您复制了一个示例,这很可能是错误的,并且是您问题的根源

4) I had the same problem. I didn't have any lines stating "import something.R". Cleaning and rebuilding also didn't solve the issue.

4)我遇到了同样的问题。我没有任何说明“import something.R”的行。清洁和重建也没有解决问题。

It turned out to be because of the name of a layout xml file. Those files can only contain lowercase letters, or numbers, or underscores. When I changed the filename (and everything that refers to it) from myLayout.xml to my_layout.xml, everything worked.

原来是因为一个布局xml文件的名字。这些文件只能包含小写字母、数字或下划线。当我将文件名(以及引用它的所有内容)从 myLayout.xml 更改为 my_layout.xml 时,一切正常。

5) I have just had this problem. It was caused because I had a redundant menu resource that referred to a non-existing string. Removed the menu XML and back came R.

5) 我刚刚遇到了这个问题。这是因为我有一个冗余的菜单资源,它引用了一个不存在的字符串。删除了菜单 XML 并返回了 R。

回答by Ednilson Campos

As has been said in this link "R cannot be resolved to a variable"?and this other R cannot be resolved - Android errorthere are several solutions.

正如此链接中所说的 “R 无法解析为变量”?而这个其他 R 无法解决 - Android 错误有几种解决方案。

In my case I configured proxy server in SDK Manager: Tools> Options

就我而言,我在 SDK Manager 中配置了代理服务器:工具>选项

回答by user280109

i was getting this error, after i updated the sdk tools and packages. i think the update hadnt worked properly, after a lot of head scratching, i just tried updating the sdk packages again using the sdk manager tool, and after that everything was ok.

在我更新了 sdk 工具和包之后,我收到了这个错误。我认为更新没有正常工作,经过大量的挠头之后,我只是尝试使用 sdk 管理器工具再次更新 sdk 包,之后一切正常。