在 Eclipse 中导入 android.R:为什么? [英] import android.R in Eclipse : Why?

查看:13
本文介绍了在 Eclipse 中导入 android.R:为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘自文档:

Eclipse 有时喜欢在使用资源的文件的顶部,尤其是当您询问 eclipse 时排序或以其他方式管理进口.这将导致您的品牌休息.留意这些错误的导入语句并删除它们.

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 一直这样做?

My question : WHY? Why does eclipse keep on doing this?

我使用 Eclipse 开发 Android 应用程序已经有一段时间了,但我一直无法理解为什么 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.

当我使用 Ctrl + Shift + O 来组织我的导入语句时,import android.R 自动添加.突然间我的正确代码突然被红色错误覆盖,说R无法解析.对于初学者来说,这真的很可怕,因为他不知道自己做错了什么.

When I use Ctrl + Shift + O to 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.

在另一种情况下,假设我的布局文件有问题并且没有生成 R.java,它说 R 无法解析,因为 R.由于错误,java尚未生成.当我将光标移动到任何错误时,它建议我import android.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.

在 Android 上工作一段时间后,我知道永远导入 android.R,但我一直无法理解为什么 eclipse 一直建议它,坦率地说,添加import android.R 从来没有解决过我的任何问题.它只是增加了现有的问题,这在开发的最初几天非常痛苦.

After working on Android for quite sometime now, I know that never to 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.

那么,有没有人知道 eclipse 提出错误导入建议的原因?这只是一个错误吗?我不认为这是一个错误,因为至少在 Android 文档中提到它之后它会得到修复.

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.

如果不是bug,那么android.R的真正目的是什么?它究竟指的是什么?

If it's not a bug, then what is a real purpose of android.R? What does it exactly refer to?

您的意见/经验会很有帮助!

Your opinions/experiences will be really helpful!

谢谢!

推荐答案

这不是错误.在某些情况下,android.R 可以提供帮助并解决问题.

This is not a bug. There are a few instances where android.R can be helpful and solve problems.

android.R 是一个 R.java 文件,就像您在自己的项目中拥有的文件一样.项目中的一个 (your.packagename.R) 保存对 /res 文件夹下资源的引用,如布局、可绘制对象、XML 文件、原始文件、字符串等

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 文件包含对 Android 内置的某些默认资源的引用,例如 simple_list_item_1.

On the other hand, the android.R file holds references to certain default resources that Android has inbuilt, like simple_list_item_1.

Eclipse 建议这样做,并且有时会自动导入它,就像您的项目的 R 文件由于 XML 错误或其他原因尚未生成一样,您的代码将引用一个不存在的文件.通过导入 android.R,eclipse 确保您的代码引用一个存在的类.但是,android.R 不太可能拥有与您相同的资源,这将引发另一组错误.

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 中导入 android.R:为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆