R.java 文件背后的魔力

发布于 2024-08-17 17:50:01 字数 390 浏览 6 评论 0原文

最近我在使用 R.java 文件时遇到了一些问题。现在我决定进行备份并删除该文件,看看会发生什么。

什么也没发生,所以我创建了一个空的 R.java 文件并希望一切顺利。现在 Eclipse 似乎发现该文件已被篡改,甚至发出警告:

R.java was modified manually! Reverting to generated version!

仅此而已。我尝试手动构建它,但没有结果。

所以,我有两个问题:

  1. 文件
  2. 我应该怎么做才能强制 Eclipse 重新生成这里发生的 ?文件是如何创建的,生成文件的代码在哪里?

我将不胜感激任何帮助。像往常一样,问题发生在截止日期前几天:)

Recently I have been having quite some problems with R.java file. Now I have decided to do a backup and delete the file to see what happens.

Nothing happened, so I created an empty R.java file and hoped for the best. Now Eclipse seems to figure out that the file was tampered with and even issues a warning:

R.java was modified manually! Reverting to generated version!

And that's all there is. I tried building it manually but got no results.

So, I have two questions:

  1. what should I do to force Eclipse to regenerate the file
  2. what is happening here? How is the file created, where is the code that is generating the file?

I would appreciate any help. As usual the problem occurred just a few days before the deadline :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

筱武穆 2024-08-24 17:50:01

我在编译 Android 应用程序时遇到了问题,似乎无法生成 R.java 的莫名其妙的错误。

该问题是由于我之前在构建过程中杀死了 Android 模拟器而引起的。不要那样做!一个线索是几个剩余的 *.out.xml 文件的出现。

解决方案是选择项目,单击“项目”->“清理”,然后手动删除所有 *.out.xml 文件。只有这样我才能点击运行->运行并成功构建。

另一件事:我正在使用 Eclipse。我必须确保在 Package Explorer 中选择了该项目,而而不是只是一些随机源文件。很容易不被注意到!

I had a problem getting my Android app to compile, with seemingly inexplicable errors about failure to generate R.java.

The problem was caused by my previously killing the Android emulator during the build. Don't do that! A clue was the appearance of several left over *.out.xml files.

The solution was to select the project, click Project->Clean, and then manually delete all the *.out.xml files. Only then could I click Run->Run and have a successful build.

One more thing: I'm using Eclipse. I had to make sure that the project was selected in Package Explorer, and not just some random source file. Easy to not notice!

聊慰 2024-08-24 17:50:01

R.java 在构建时自动生成。它的内容基于资源文件(包括布局和首选项)。

当你删除它时,它会被重新创建,但如果你创建自己的,你会遇到麻烦,因为构建系统不会替换它。

您可以简单地再次删除它(手动或通过选择“项目”->“清理”)并重建您的程序。

R.java is autogenerated on build. It's content is based on the resource files (including layouts and preferences).

When you deleted it, it was recreated, but if you create your own, you will get into trouble as the build system will not replace it.

You can simply delete it again (manually or by choosing Project->Clean) and rebuild your program.

夜光 2024-08-24 17:50:01

我遇到过这个问题好几次,大多数时候是由“res”文件中的一些错误引起的,检查所有res文件(主要是XML)中的一切是否正常,然后就可以构建了。

例如,此问题的两个常见原因可能是:

  • 没有正确拼写 XML 文件中样式中的属性名称。

  • 未正确关闭区域设置文件的 XML 中的字符串。

I had this problem several times, and most of the times it is caused by some mistake in the "res" files, check everything is OK in all your res files, mainly XML, and after you can built.

For example, two common causes of this problem can be:

  • Didn't properly spell the name of a property in a Style in a XML file.

  • Didn't properly close a string in the XML of a locale file.

只是一片海 2024-08-24 17:50:01

我发现了问题 - 我创建了一个 9patch 图像,其补丁区域仅在 Y 轴上定义(我试图防止在另一个图像上调整大小)。

问题仍然存在:为什么没有关于错误的警告?或者如果有的话——它埋在哪里?

I found the problem - I have created a 9patch image with the patch area defined only on the Y axis (I was trying to prevent resizing on the other one).

The question remains: Why isn't there a warning about the error? Or if there is - where is it buried?

回忆躺在深渊里 2024-08-24 17:50:01

抱歉,请尝试在 Eclipse 中检查输出。可能你有一些错误阻止 Eclipse 完成构建过程(检查你的 AndroidManifest.xml,它是否正确?)。我的意思是,你应该检查你的所有资源文件(string.xml,布局,...)和接口(*.aidl),可能存在一些问题(语法不一致,或者其他......)阻止eclipse生成R.java。

Sorry, but try to check the output in Eclipse. May be you have some errors that prevents Eclipse to finish build proccess(check your AndroidManifest.xml, is it correct?).I mean, that you should check all your resource files(string.xml, layouts, ...) and interfaces(*.aidl), may be there are some problems(inconsistent syntax, or something....) that prevent eclipse to generate R.java.

故人如初 2024-08-24 17:50:01

我遇到了同样的问题,那就是我使用大写字母命名了资源文件(.png 图像)。 (ImageButton001.png 而不是 imagebutton001.png)。

更改文件名后,我的 R.java 又回来了。

问候,
胡安

I had the same issue and it was that I had named a resource file (a .png image) using uppercase letters. (ImageButton001.png instead of imagebutton001.png).

Afther changing the name of the file my R.java came back.

Rgards,
Juan

身边 2024-08-24 17:50:01

如果可用内存不足,构建工具将默默失败(Eclipse 不会显示任何错误或在错误日志中报告任何内容)。关闭其他打开的应用程序(例如模拟器和浏览器),然后在应用程序清单文件中添加一个空格并保存。

If there's insufficient free memory the build tool will silently fail (Eclipse will not show any errors or report anything in the error log). Close your other open applications (e.g. the emulator and browser) then add a space to the application manifest file and save it.

帅哥哥的热头脑 2024-08-24 17:50:01

正如 ponkin 所说 - 你可能有一个问题导致 R 无法重新生成自身,因此你现在有一百万零一个问题。

不要尝试自己创建 R 类。尝试回想一下您最近弄乱的 XML,并检查它是否有错误

我也遇到过同样的问题 此处

As ponkin stated - you probably have one single problem that causes R not to re-generate itself, because of which you now have one million and one problem.

Do not try to create R class yourself. Try to think of the last XML you've been messing with, and check it for errors

I've had the same problem here

柠檬色的秋千 2024-08-24 17:50:01

确保您的清单文件没有错误。
如果删除/res/values/strings.xml中的资源字符串app_name,它不知道如何正确生成R.java。
我在做 HelloSpinner 教程时发现了这个问题。

Make sure there are no errors is your Manifest file.
If you delete the resource string app_name in /res/values/strings.xml, it doesn't know how to generate R.java right.
I found this a problem when doing the HelloSpinner tutorial.

小嗲 2024-08-24 17:50:01

我发现不需要的资源导致了问题。我一删除资源 R.java 就生成了。

I found that an unwanted resource was causing an issue. As soon as I deleted the resource R.java was generated.

与往事干杯 2024-08-24 17:50:01

我遇到了这个问题,要重建 R.java,我剪切了我刚刚处理的 xml 文件中的代码(如果这不起作用,请尝试其他文件),然后删除该文件创建另一个同名的并将代码粘贴回其中。希望这有帮助。

I've had this problem, to rebuild R.java I cut the code in the xml file I've just been working on (or try others if this doesn't work), deleting the file then creating another with the same name and pasting the code back into it. Hope this helps.

三月梨花 2024-08-24 17:50:01

值得一提的是,如果 XML 文件包含 ?,则不会重新生成 R.java,并且错误不会显示在 XML 文件中(有时会在一段时间后显示)
我有一行类似于:

<字符串名称=“问号”\>?< /字符串>

尝试更改 ?到其他东西,看看 R.java 文件发生了什么 - 对我有用

Something that can be good to know is that if an XML file contains a ?, then R.java is not regenerated and the error is not shown in the XML file (sometimes it shows up after a while)
I you have a line similar to:

< string name="questionmark"\>?< /string> <br>

try to change the ? to something else and see what happens to the R.java file - worked for me

jJeQQOZ5 2024-08-24 17:50:01

我遇到了类似的问题,我用大写字母重命名了 Raw 文件夹中的 .wav 文件。当我更改为全部小写时,清理了项目并正常重建了 R.java 。

I had a similar problem, I had renamed a .wav file in the Raw folder with a capital letter. When I changed to all lower case, cleaned the project and it rebuilt R.java fine.

木森分化 2024-08-24 17:50:01

正如其他人所说,不要创建自己的 R.java 文件。我刚开始使用Android时也遇到了同样的问题,花了一些时间才弄清楚编译错误导致的原因。

您想对 R.java 文件做什么?

As others stated, stay away from creating your own R.java file. I ran into the same problem when I first started with Android, and it took some time to figure out what caused by compilation errors.

What were you trying to do with the R.java file?

时光是把杀猪刀 2024-08-24 17:50:01

我也有同样的问题。我刚刚访问:

Project -> Properties -> Android 

并为我的项目选择了正确的模拟器,当时我有 3 个不同的模拟器可供选择。我意识到我在某个特定时刻更改了模拟器。因此尝试更改模拟器,这可能会有所帮助!

祝你好运

I had the same problem. I just went to:

Project -> Properties -> Android 

and chose the right Emulator for my project, I had 3 different to choose from in that moment. I realized that i changed the Emulator in some given moment. So try to change the Emulator, that might help!

Good luck

樱花落人离去 2024-08-24 17:50:01

我对 IntelliJ 也有同样的问题。问题是我在 strings.xml 中定义了两次 app_name 由于某种原因,IDEA 检查没有自动检测到它。

I had the same issue with IntelliJ. The problem was that I had app_name defined twice in strings.xml For some reason, IDEA inspections didn't pick this up automatically.

一城柳絮吹成雪 2024-08-24 17:50:01

我在另一个网站上读到了这篇文章,它对我有帮助:

“如果这不能解决您的问题,请尝试修改您的 AndroidManifest.xml (只需添加一个空格并保存)。我听说这会强制要重建的 R.java

因此只需在清单文件中添加一个空格,然后重建即可!

I read this on another site and it helped me:

"If that doesn't fix your problem, try modifying your AndroidManifest.xml (just add a space and save). I've heard that will force the R.java to rebuild. "

So just add a space in the manifest file and then rebuild!

活泼老夫 2024-08-24 17:50:01

附带说明一下,通常这些 *.out.xml 文件是通过尝试构建项目而生成的,而 xml 文件则集中在编辑器中

As a side note, usually those *.out.xml files are generated by trying to build your project whilst an xml file is focused in the editor

执妄 2024-08-24 17:50:01

我有类似的问题。清理项目没有解决问题,代码中没有错误,文件名中也没有大写或空格。

解决方案是当您选择“项目”-->“清理”然后取消选中“立即开始构建”选项时,会出现一个弹出窗口。清理项目。关闭并重新打开 Eclipse,然后构建项目(PROJECT-->BUILD)。

问题仍然是,为什么在不关闭 Eclipse 的情况下进行简单的清理和构建不起作用。

I had similar problem.Cleaning project didn't solve the issue and there was no error in the code or the uppercase or spaces in file names.

The solution was going to a popup window which came when you choose PROJECT-->CLEAN and then UNcheck the "Start a build immediately" option. Clean the project. Close and reopen Eclipse and then build the project (PROJECT-->BUILD).

The question remains why simple clean and build without closing Eclipse didn't worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文