eclipse 从不同的包访问 R.java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6186763/
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
Accessing R.java from different packages
提问by MM.
I have an application divided in subpackages, just for personal organization:
我有一个应用程序分为子包,仅用于个人组织:
com.myname.myapp
|
`- com.myname.myapp.activities
|
`- com.myname.myapp.whatever
|
`- ...
The problem is that the generated R.java is located at com.myname.myapp
and thus when I type R.id.something
in a class from the subpackage com.myname.myapp.activities
, I get R cannot be resolved to a variable
(obvious I guess).
问题是生成的 R.java 位于,com.myname.myapp
因此当我R.id.something
从子包中输入一个类时com.myname.myapp.activities
,我得到R cannot be resolved to a variable
(显然我猜)。
When I click on Organize imports(Ctrl+Shift+O), Eclipse fixes it adding import com.myname.myapp.R
at the top, and everything seems to work perfectly. But on the other hand, Android documentationstates this:
当我单击“组织导入”(Ctrl+Shift+O) 时,Eclipse 将其修复添加import com.myname.myapp.R
到顶部,并且一切似乎都运行良好。但另一方面,Android 文档说明了这一点:
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 中断。留意这些错误的导入语句并删除它们
Knowing that everything is working perfectly, what should I do?
知道一切正常,我该怎么办?
回答by Kakey
You can import R.file where ever you want.
你可以在任何你想要的地方导入 R.file。
import com.myname.myapp.R;
or else use at the variable like this
或者像这样在变量上使用
com.myname.myapp.R.id.test
回答by Dinesh Sharma
R.javais always created at the location(package) mentioned by you in the manifest file.
R.java总是在您在清单文件中提到的位置(包)创建。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myname.myapp"
android:versionCode="1"
android:versionName="1.0">
For any other location or package , you must import R.java file from the root location.
对于任何其他位置或包,您必须从根位置导入 R.java 文件。
Hope you understood the issue.
希望你理解这个问题。
回答by phantom
The warning has bearance on the fact, there are two versions of the R class. There is a generic version that comes as part of the android packages (android.R) and there is the project specific one that represents your local resource files (com.myname.myapp.R). Just see them as you would the two versions of the Date classes in the java standard library.(java.util.Date & java.sql.Date) two classes, with the same name, specified by package.
该警告与事实有关,R 类有两个版本。有一个通用版本作为 android 包 (android.R) 的一部分提供,并且有一个项目特定版本代表您的本地资源文件 (com.myname.myapp.R)。只需将它们视为 Java 标准库中 Date 类的两个版本。(java.util.Date & java.sql.Date) 两个具有相同名称的类,由包指定。