Android R.java 文件未使用 UI 元素的 ID 更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2487263/
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
R.java file not updating with UI element's id
提问by Mohit Deshpande
I added some UI elements to the main.xml file in the res\layout folder and when I try to access them through the call, R.layout.my_uielement
, the UI element that I created is not there. Even when I add a new xml file with UI elements, it still doesn't show up in the R.layout
class. I have made checked the ids on them and they have the correct format (I think): android:id="@+id/my_button"
. What could be the problem? Do I need to compile the code first?
UPDATE:
I have already tried adb kill-server
then adb start-server
. It doesn't seem to help. I have also tried R.id.my_uielement
, it doesn't register either.
我在 res\layout 文件夹中的 main.xml 文件中添加了一些 UI 元素,当我尝试通过调用访问它们时R.layout.my_uielement
,我创建的 UI 元素不存在。即使我添加了一个带有 UI 元素的新 xml 文件,它仍然没有显示在R.layout
类中。我已经检查了它们的 ID,它们的格式正确(我认为):android:id="@+id/my_button"
. 可能是什么问题呢?我需要先编译代码吗?
更新:那时
我已经试过了。它似乎没有帮助。我也试过,它也没有注册。adb kill-server
adb start-server
R.id.my_uielement
回答by Rich
This may not be the case for you, but sometimes when I use something from the android.R package (default android resources) and I use it in my class before making reference to anything from my own resource package, Eclipse adds the import android.R;
statement in my imports. This will conflict with the default behavior of implicitly looking in your package's resources.
这对您来说可能不是这种情况,但有时当我使用 android.R 包中的某些内容(默认 android 资源)并且在引用我自己的资源包中的任何内容之前在我的类中使用它时,Eclipse 会import android.R;
在我的进口。这将与隐式查看包资源的默认行为发生冲突。
Check your import statements for import Android.R;
. If it's there, remove it. And, if you're using anything from android.R, fully qualify it when you use it in code.
检查您的导入语句import Android.R;
。如果它在那里,请将其删除。而且,如果您使用 android.R 中的任何内容,请在代码中使用它时对其进行完全限定。
回答by bootkick
Clean project works fine. In Eclipse if you just change some xml components, it doesn't always generate a new R file. Cleaning does it.
干净的项目工作正常。在 Eclipse 中,如果您只是更改一些 xml 组件,它并不总是生成新的 R 文件。清洁做到了。
回答by fhucho
Use R.id.your_ui_element, not R.layout.your_element
使用 R.id.your_ui_element,而不是 R.layout.your_element
回答by Pramod
I faced this problem too. The R.java was neither generated nor updated.
我也遇到了这个问题。R.java 既没有生成也没有更新。
If R.java is not generatedwhen you create the project, run your 'empty` project. An R.java with references to resources will be automatically generated.
如果创建项目时未生成R.java ,请运行“空”项目。将自动生成包含资源引用的 R.java。
If R.java is not getting updated, Check for updates in Help -> Check for Updates in Eclipse and update your SDK and ADT if required.
如果 R.java 未更新,请在 Help -> Check for Updates in Eclipse 中检查更新,并在需要时更新您的 SDK 和 ADT。
回答by SkyTreasure
After 2 days I found a way to fix it...
2天后,我找到了解决它的方法......
I had saved half done project in my mail, so when R.java
vanished i just replaced it with the old R.java
file, this fixed a lot of errors, then made necessary changes and built the project(build it not clean)...I do not know whether clean and build performs the same job or not..but everytime i cleaned it , R.java
used to go invisible...
我在我的邮件中保存了一半完成的项目,所以当R.java
消失时我只是用旧R.java
文件替换它,这修复了很多错误,然后进行了必要的更改并构建了项目(构建不干净)......我不知道清洁和构建是否执行相同的工作......但每次我清理它时,R.java
过去都是隐形的......
回答by Neon
Cleanis working because it BUILDthe project after cleaning. So the problem is ( at least it was in my case, because I switched off the Automatic Build a few days earlier ) that after you add an item or anything in a .xml file, the eclipse doesn't build it and can't refresh the R.java .
Clean正在工作,因为它在清理后构建了项目。所以问题是(至少在我的情况下是这样,因为我几天前关闭了自动构建)在 .xml 文件中添加项目或任何内容后,eclipse 不会构建它并且不能刷新 R.java 。
回答by Topo
None of these worked for me!
The only way i can access an @+id/foo
is by myPackage.R.id.foo
.
这些都不适合我!我可以访问 an 的唯一方法@+id/foo
是通过myPackage.R.id.foo
.
回答by Dean
I've noticed that my R.java file is not always being properly updated when I add items to my strings.xml file or elsewhere. If I use a new value I added like R.string.newString the actual integer value might still be pointing to R.string.oldString or whatever (it seems to usually be the element directly above or below the new element in strings.xml but I haven't nailed down the pattern for sure yet).
我注意到当我将项目添加到我的 strings.xml 文件或其他地方时,我的 R.java 文件并不总是正确更新。如果我使用像 R.string.newString 这样添加的新值,则实际的整数值可能仍指向 R.string.oldString 或其他任何内容(它似乎通常是 strings.xml 中新元素的正上方或下方的元素,但是我还没有确定模式)。
You're not supposed to modify the R.java file, but it doesn't say anything about deleting it (not that I've seen, anyway). I've found that deleting R.java usually fixes my resource-related problems because Eclipse immediately regenerates the file with the integer values properly assigned.
你不应该修改 R.java 文件,但它没有说明删除它(无论如何我都没有看到)。我发现删除 R.java 通常可以解决我的资源相关问题,因为 Eclipse 会立即使用正确分配的整数值重新生成文件。
Please note, I'm very much a beginner at Eclipse so I may not understand the consequences of deleting R.java but it seems to be working well for me so far. If what I'm doing is very bad, please let me know.
请注意,我是 Eclipse 的初学者,所以我可能不明白删除 R.java 的后果,但到目前为止它似乎对我来说效果很好。如果我做的很糟糕,请告诉我。