从不同的包访问 R.java

Accessing R.java from different packages(从不同的包访问 R.java)
本文介绍了从不同的包访问 R.java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个应用程序分为子包,仅用于个人组织:

I have an application divided in subpackages, just for personal organization:

com.myname.myapp
 |
 `-  com.myname.myapp.activities
 |
 `-  com.myname.myapp.whatever
 |
 `-  ...

问题是生成的 R.java 位于 com.myname.myapp,因此当我在子包中的类中键入 R.id.somethingcom.myname.myapp.activities,我得到 R 无法解析为变量(我猜很明显).

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).

当我点击 Organize imports (Ctrl+Shift+O) 时,Eclipse 修复它,在顶部添加 import com.myname.myapp.R,一切看起来完美地工作.但另一方面,Android 文档 指出:

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 documentation states this:

Eclipse 有时喜欢在使用资源的文件顶部添加 import android.R 语句,尤其是当您要求 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

知道一切正常,我该怎么办?

Knowing that everything is working perfectly, what should I do?

推荐答案

你可以在任何你想要的地方导入 R.file.

You can import R.file where ever you want.

import com.myname.myapp.R; 

或者像这样在变量处使用

or else use at the variable like this

com.myname.myapp.R.id.test

这篇关于从不同的包访问 R.java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Show progress during FTP file upload in a java applet(在 Java 小程序中显示 FTP 文件上传期间的进度)
How to copy a file on the FTP server to a directory on the same server in Java?(java - 如何将FTP服务器上的文件复制到Java中同一服务器上的目录?)
FTP zip upload is corrupted sometimes(FTP zip 上传有时会损坏)
Enable logging in Apache Commons Net for FTP protocol(在 Apache Commons Net 中为 FTP 协议启用日志记录)
Checking file existence on FTP server(检查 FTP 服务器上的文件是否存在)
FtpClient storeFile always return False(FtpClient storeFile 总是返回 False)