从不同的包访问 R.java

时间:2022-10-28
本文介绍了从不同的包访问 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:带有 Android Facebook SDK 的 keytool 下一条:什么是“已保存实例状态"?

相关文章

最新文章