在 Eclipse 中导入 android.R:为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15309941/
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
import android.R in Eclipse : Why?
提问by Swayam
An excerpt from the documentation:
文档摘录:
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 有时喜欢在使用资源的文件顶部添加 import android.R 语句,尤其是当您要求 Eclipse 排序或以其他方式管理导入时。这将导致您的 make 中断。注意这些错误的导入语句并删除它们。
My question : WHY?Why does eclipse keep on doing this?
我的问题:为什么?为什么 eclipse 会继续这样做?
I have been developing Android applications using Eclipse for a quite some time now but I have never been able to understand why eclipse does such a thing.
我使用 Eclipse 开发 Android 应用程序已经有一段时间了,但我一直无法理解为什么 Eclipse 会做这样的事情。
When I use Ctrl+ Shift+ Oto organize my import statements, import android.R
gets added automatically. And all of a sudden my correct code is suddenly covered in red errors, saying that R cannot be resolved. It can get really scary for a beginner as he has no idea what he did wrong.
当我使用Ctrl+ Shift+O来组织我的import语句,import android.R
就会自动添加。突然间我的正确代码突然被红色错误覆盖,说R 无法解析。对于初学者来说,这可能会变得非常可怕,因为他不知道自己做错了什么。
In another scenario, suppose there is something wrong with my layout files and R.java
is not being generated, it says that R cannot be resolved, as R.java has not been generated due to the errors. As I move my cursor to any of the errors, it suggests me to import android.R
.
在另一种情况下,假设我的布局文件有问题并且R.java
没有生成,它说R 无法解析,因为 R.java 由于错误而没有生成。当我将光标移动到任何错误时,它建议我将import android.R
.
After working on Android for quite sometime now, I know that neverto import android.R, but what I have never been able to understand why eclipse keeps on suggesting it, as frankly speaking, adding import android.R never solved any problem of mine. It just added to the existing problems, which used to be really painful during initial days of development.
在 Android 上工作了很长一段时间后,我知道永远不要导入 android.R,但是我一直无法理解为什么 eclipse 一直建议它,坦率地说,添加 import android.R 从未解决我的任何问题. 它只是增加了现有的问题,在开发的最初几天,这些问题曾经非常痛苦。
So, does anyone know the reason behind eclipse making the suggestion to make an incorrect import? Is it just a bug? I don't think it's a bug, as it would have got fixed at least after it was mentioned on the Android documentation.
那么,有没有人知道 eclipse 提出错误导入建议的原因?这只是一个错误吗?我不认为这是一个错误,因为至少在 Android 文档中提到它之后它会得到修复。
If it's not a bug, then what is a real purpose of android.R
? What does it exactly refer to?
如果这不是错误,那么真正的目的是android.R
什么?它具体指的是什么?
Your opinions/experiences will be really helpful!
您的意见/经验将非常有帮助!
Thanks!
谢谢!
采纳答案by Raghav Sood
This is not a bug. There are a few instances where android.R
can be helpful and solve problems.
这不是错误。有一些实例android.R
可以提供帮助并解决问题。
android.R
is an R.java file like the one you have in your own projects. The one in your projects (your.packagename.R
) holds references to the resources you have under your /res
folder like layouts, drawables, XML files, raw files, strings etc.
android.R
是一个 R.java 文件,就像您在自己的项目中拥有的文件一样。项目中的 ( your.packagename.R
) 包含对/res
文件夹下资源的引用,如布局、可绘制对象、XML 文件、原始文件、字符串等。
On the other hand, the android.R
file holds references to certain default resources that Android has inbuilt, like simple_list_item_1
.
另一方面,该android.R
文件包含对 Android 内置的某些默认资源的引用,例如simple_list_item_1
.
Eclipse suggests this and auto imports this sometimes as if your project's R file hasn't been generated due to an XML error or something, your code will be referencing a file that doesn't exist. By importing android.R
, eclipse makes sure your code references a class that exists. However, android.R
is unlikely to have the same resources you did, and this will raise another set of errors.
Eclipse 建议这样做,并且有时会自动导入它,就像由于 XML 错误或其他原因而未生成项目的 R 文件一样,您的代码将引用不存在的文件。通过导入android.R
,eclipse 确保您的代码引用一个存在的类。但是,android.R
不太可能拥有与您相同的资源,这将引发另一组错误。
回答by LilkeMan
Eclipse will also always try to automatically import android.R in your class if you rename your package name via android tools and not rename base package name in your code with the same name because he will assume you have two R's in your file.
如果您通过 android 工具重命名您的包名,并且不使用相同名称重命名代码中的基本包名,Eclipse 也将始终尝试在您的类中自动导入 android.R,因为他会假设您的文件中有两个 R。
回答by hassan yang
Try this...
尝试这个...
Window -> Prefs -> Java -> Editor -> Save Actions
窗口 -> 首选项 -> Java -> 编辑器 -> 保存操作
Uncheck "Organize Imports." Hopefully that will do it.
取消选中“组织导入”。希望那会做到。
After this setting, it works for me.
在此设置后,它对我有用。