Android - 如何重新生成 R 类?

发布于 2024-08-17 14:30:04 字数 301 浏览 5 评论 0原文

可能的重复:
在 Eclipse 中进行 Android 开发:R.java 未生成

我已将一个项目导入到 Eclipse IDE 中,但由于 R 文件未自动生成,因此出现错误。

如何编辑 R 文件以使其符合我的项目要求?

Possible Duplicate:
Developing for Android in Eclipse: R.java not generating

I have imported a project into my Eclipse IDE, but it's giving me an error since R file is not generated automatically.

How can I edit the R file so that it matches my project requirements?

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

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

发布评论

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

评论(26

旧夏天 2024-08-24 14:30:05

对我来说,这是一个未定义的字符串值资源……在我手动将其添加到 Strings.xml 后,R 类会自动重新生成!

For me, it was a String value Resource not being defined … after I added it by hand to Strings.xml, the R class was automagically regenerated!

不念旧人 2024-08-24 14:30:05

从现有项目导入时通常会出现此问题。尝试修复 res 文件夹中的所有 .xml 文件以及 Manifest 文件中的所有错误。错误很可能显示在“ma​​tch_parent”参数中,请将其更改为“fill_parent”。当所有错误都清除后,就会出现R.java。

如果任何 .xml 文件都没有错误。尝试编辑任何 xml 文件(例如删除一个字母并重新输入),然后保存该文件,这将生成 R.java。

This problem normally happens when importing from an existing project. Try to fix all errors from all .xml files in res folder, as well as Manifest file. Most likely the errors are shown at "match_parent" parameter, change it to "fill_parent". When all errors are clear, R.java will be appeared.

If there is no error on any .xml file. Try edit any xml file (like delete a letter and type it back) and then save the file, this will cause to generate the R.java.

流年里的时光 2024-08-24 14:30:05

我有一个问题,findViewById(R.id.edit_text) 但返回的是 TextView 而不是 EditText。我认为问题是 R.id 类的 id 号错误,然后,我清理该项目,然后它就可以正常工作了。

I have a problem that the findViewById(R.id.edit_text) but return a TextView not a EditText. I think the problem is the R.id class has wrong id number, then, i clean the project then its works fine.

吐个泡泡 2024-08-24 14:30:05

就我而言,是一个命名不正确的 XML 文件导致我的 R 文件无法重建。这没有显示在包资源管理器中,但显示在 LogCat 中。请留意有关您的 res 文件的任何警告。无论您清理项目多少次,在解决这些错误之前,R 文件都不会重建。

In my case it was an improperly named XML file that caused my R file not to rebuild. This did not show up in the package explorer, but it was shown in the LogCat. Look out for any warnings there about your res files. No matter how many times you clean your project, the R file will not rebuild until those errors are taken care of.

十年不长 2024-08-24 14:30:04

在另一个 stackoverflow 问题中,有人回答了这个:你必须消除 xml 文件中的警告和错误(语法和链接)。这加上做一个干净/构建项目。

项目->清理,然后选择构建项目的选项。

In another stackoverflow question, somebody answered this : you must get rid of warnings and errors (syntax and links) in your xml files. This plus doing a clean / build project.

Project -> Clean, and choose the option to build the project.

傲影 2024-08-24 14:30:04

我发现这种情况发生在我身上,布局损坏,一切都崩溃了。放松,这就像你第一次学习编程时犯的老错误一样,你忘记了一个分号,就会产生一百个错误。许多人惊慌失措,按下所有按钮,使事情变得更糟。

解决方案

  • 确保 R. 链接到的任何内容都没有损坏。如果它损坏,ADK 可能无法重新生成 R。修复 XML 文件中的所有错误
  • 如果您不知何故碰到了某些东西并在 Activity 中创建了 import android.R,请将其删除
  • 运行项目 ->干净。这将删除并重新生成 R 和 BuildConfig。
  • 确保项目->自动构建被勾选。
  • 等待几秒钟,错误就会消失。
  • 如果它不起作用,请删除 /gen/ 文件夹中的所有内容
  • 如果它仍然不起作用,请尝试右键单击您的项目 -> Android 工具 ->修复项目属性。

如果 R 无法重新生成该怎么办

这通常发生在您的 xml 文件损坏时。

  • 检查 XML 文件中的错误,主要是 /res/ 文件夹中的错误
  • 常见位置是 /layout/ 和 /values/,特别是如果您最近更改了其中之一
  • 检查 AndroidManifest.xml,我发现经常更改字符串,然后忘记更改 AndroidManifest.xml 中的字符串名称。
  • 如果找不到问题。 右键单击/gen/ ->从本地历史恢复... ->勾选 R.java ->单击“恢复”。这并不能解决问题,但会清除额外的错误,使问题更容易找到。

我知道这里已经有很多答案了,但这是 Google 上的第一个链接,所以我在这里整理了所有建议,希望它能帮助其他对此不熟悉的人:)

I found this happening to me with a broken layout and everything blows up. Relax, it's like that old mistake when you first learned programming where you forget one semicolon and it generates a hundred errors. Many panic, press all the buttons, and makes things worse.

Solution

  • Make sure that anything the R. links to is not broken. If it's broken, ADK may not regenerate R. Fix all errors in your XML files.
  • If you somehow hit something and created import android.R in your activity, remove it.
  • Run Project -> Clean. This will delete and regenerate R and BuildConfig.
  • Make sure Project -> Build Automatically is ticked.
  • Wait a few seconds for the errors to disappear.
  • If it doesn't work, delete everything inside the /gen/ folder
  • If it still doesn't work, try right-clicking your project -> Android Tools -> Fix Project Properties.

What to do if R doesn't regenerate

This usually happens when you have a broken xml file.

  • Check errors inside your XML files, mainly within the /res/ folder
  • Common places are /layout/ and /values/ especially if you've changed one of them recently
  • Check AndroidManifest.xml, I find that often I change a string, and forget to change the string name from AndroidManifest.xml.
  • If you can't find the issue. right click /gen/ -> Restore from local history... -> tick R.java -> click Restore. This doesn't solve the problem, but it will clear out the extra errors to make the problem easier to find.

I know there's already a lot of answers here, but this is the first link on Google, so I'm compiling all the advice here and hope it helps someone else new to this :)

不即不离 2024-08-24 14:30:04

好的,我修复了它:

当我将清单更改为目标 1.5 版本时,出现了一百万个错误,并且只有一个与 R 类不存在无关 - 在清单文件属性“targetSdkVersion”和“maxSdkVersion”中,sdk 1.5 中不存在

因为这个 R 类无法生成。

Ok, I fixed it:

When I changed manifest to target 1.5 version, million errors appeared and only one wasn't related to inexistance of R class - in manifest file attributes "targetSdkVersion" and "maxSdkVersion" did not exist in sdk 1.5

Because of this R class was not able to generate.

时光暖心i 2024-08-24 14:30:04

我遇到了类似的问题,在添加一些图标和布局文件后,R.java 文件没有重新生成。我尝试清理和重建,但事情变得更糟,因为原始的 R.java 文件消失了并且仍然没有重新生成。最终我意识到生成 R.java 的编译器一定存在编译器错误,而我们在控制台中没有看到这些错误。幸运的是,我之前注意到,

  1. 如果一个 .xml 文件引用另一个不存在的 .xml 文件,这是一个错误,并且
  2. 文件名中包含大写字母也是一个错误(为什么禁止这样做,我喜欢驼峰式大小写)。

不管怎样,在将我的图标和 .xml 文件重命名为全部小写,然后修复 .xml 文件中的所有引用后,瞧,R.java 被重新生成了。当我停下来吃午饭时,正好感觉良好:-)

I had a similar problem where the R.java file was not being regenerated after I'd added some icons and layout files. I tried cleaning and rebuilding and things got worse since then the original R.java file was gone and still wasn't regenerated. Eventually it dawned on me that there must be compiler errors for the compiler that generates R.java and we aren't being shown those in the console. Fortunately, I had noticed earlier that

  1. it's an error if one .xml file refers to another which is non-existent and
  2. it's an error to have upper case letters in your file names (why is that forbidden, I LOVE camel case).

Anyways, after renaming my icons and .xml files to all lowercase and then fixing all the references inside the .xml files, voila, R.java was regenerated. Just in time to feel good when I stopped for lunch :-)

小ぇ时光︴ 2024-08-24 14:30:04

要再次生成 R 文件:

  1. 修改布局->activity_main.xml 中的内容
  2. 保存文件
  3. Project->Clean...

只需删除位于 gen 目录中的 R 文件,然后尝试 Project->Clean。
然后再次构建您的应用程序。

To generate the R file again:

  1. Modify something in layout->activity_main.xml
  2. Save the file
  3. Project->Clean...

Just delete the R file located in gen directory, and then try Project->Clean.
Then build your app again.

陌上芳菲 2024-08-24 14:30:04

您还可以右键单击项目 -> Android 工具 ->修复项目属性。这应该会导致重新生成 R.java 类。

You can also right click on the project -> Android Tools -> Fix Project Properties. That should cause the R.java class to be re-generated.

落花浅忆 2024-08-24 14:30:04

我重新打开 Eclipse,编辑我的一个 xml 文件,然后发现我的 activy 类引用了 android.R 而不是我的包“R”:-)

I reopen eclipse, edit one of my xml files and then discover that my activy class was refering android.R instead of my package "R" :-)

杯别 2024-08-24 14:30:04

我的问题出在 Manifyst 文件中。
我已经删除了启动器图标,并且没有在 Manifyst 上引用新图标:)

My problem was in Manifiest file.
I have deleted launcher icons and did not reference the new ones on Manifiest :)

梦里南柯 2024-08-24 14:30:04

为了重新生成 R.class,您必须尝试多种技术。

  • 从 java 源文件中删除任何 R 导入: import R.android
  • 确保通过转到 Window->Show view->Problems 打开“Problems”面板
  • 。您的 xml 资源,您将需要更正这些错误。就我而言,我必须更改我的 id 属性以包含“+”号,如下所示: android:id="@+id/grp_txt"
  • 现在继续执行 Project->Clean
  • 最后执行 Project ->关闭,项目->打开

“+”号表示新的资源 id,并强制 aapt 工具在 R.java 类中创建新的资源整数。

In order to regenerate the R.class, you will have to try multiple techniques.

  • Remove any R imports from your java source files: import R.android
  • Make sure to open the "Problems" panel by going to Window->Show view->Problems
  • If you do have some errors in your xml resources, you will need to correct those errors. In my case I had to change my id attributes to include the "+" sign as follows: android:id="@+id/grp_txt"
  • Now proceed to do Project->Clean
  • Finally do Project->Close, Project->Open

The "+" sign indicates a new resource id and the forces the aapt tool to create a new resource integer in the R.java class.

风吹雪碎 2024-08-24 14:30:04

如果你使用Intellij IDEA你可以点击Build - Rebuild Project

If you use Intellij IDEA you can click Build - Rebuild Project

等风来 2024-08-24 14:30:04

如果你不小心通过“project clean”删除了 eclipse 中的 R(不要这样做)
右键单击您的项目文件 ->从本地历史记录恢复...

重新创建您的 R

我的案例:

  • 损坏的 xml 导致 R 中出现错误。(因为 ä、ö、ü、id 中的字母)
  • 清理项目删除了我的 R。

我做了什么:

  • 本地历史记录中重新创建了 R
  • 从检查的 我的类中存在错误的 R 导入(例如 import android.R),并删除它们
  • 修复了该 xml 文件并将其内容保存在外部 txt 文件中,
  • 从我的项目中完全删除了 xml。
  • 有一次运行我的项目,
  • 将固定的 xml 放回原处

,然后……

  • 在像绿巨人一样惊慌失措后,无缘无故地给了我一个大大的拥抱。

If you accidently deleted your R in eclipse by "project clean" (don´t do this)
Rightclick on your Projectfile -> Restore from local History...

recreate your R

My Case:

  • Broken xml caused bug in R. (because of ä, ö, ü, letters in id)
  • clean project deleted my R.

What I Did:

  • recreated R from local History
  • checked for wrong R imports in my classes (like import android.R) and removed them
  • fixed that xml file and saved its content in an external txt file
  • deleted the xml completely from my project.
  • ran my project one time
  • put that fixed xml back at its place

and...

  • gave my girlfriend a big hug without a reason after freaking out like a Hulk.
酒解孤独 2024-08-24 14:30:04

我的问题是 XML 文件中存在错误。 Android 通用 XML 编辑器或 Android 通用清单编辑器并不总是自动检测到错误,因此请尝试使用常规文本编辑器。例如,不知何故,.java 文件中的一些随机乱码已插入到我的清单和 strings.xml 文件中。

右键单击任何 XML 文件(很可能位于 /res 文件夹或 AndroidManifest.xml 中),然后单击“打开方式”,然后选择“文本编辑器”。确保一切看起来都不错然后保存。另外,请确保为您的项目启用“自动构建”。我已经删除了 R.java 文件,并且它是从头开始重建的,因此无需从本地历史记录中恢复它。

My problem was errors in my XML files. Errors aren't always automatically detected in the Android Common XML Editor or Android Common Manifest Editor, so try using the regular text editor instead. For example, somehow some random gibberish from a .java file had been inserted into my manifest and strings.xml files.

Right click on any XML files (most likely in the /res folder or AndroidManifest.xml) and click "Open With," then select "Text editor." Make sure everything looks good and then save. Also, make sure to enable "Build Automatically" for your project. I had deleted my R.java file and it was rebuilt from scratch, so no need to restore it from local history.

惟欲睡 2024-08-24 14:30:04

尝试将新的“Android XML 文件”添加到例如 /res/layout 文件夹中。这可能会导致插件重新生成 R 类。

Try to add a new "Android XML file" to, for example, the /res/layout folder. This might cause the plugin to to regenerate the R class.

伊面 2024-08-24 14:30:04

你可以修改 /res 文件夹中的任何 xml 文件,甚至只是添加一个空格并保存,它就会重新生成。

You can just modify any xml files in /res folder and even just add a space and save, it will be regenerated.

请你别敷衍 2024-08-24 14:30:04

我遇到了同样的问题 - “R”丢失。这里给出的建议都没有帮助。幸运的是,我找到了原因 - Eclipse 不知何故丢失了项目属性目标。它没有设置。直到现在我还不知道是什么造成了这种情况,但将其恢复有帮助。
所以,注意 - 缺少 R 也可能意味着缺少目标

I had the same problem - "R" missing. And none of the advices given here helped. Fortunately, I've found the reason - somehow Eclipse got lost the Project-Properties-target. It was unset. I don't know till now, what had done it, but setting it back helped.
So, attention - missing R can mean the missing target, too!

月下伊人醉 2024-08-24 14:30:04

我已经尝试了上面所有的解决方案。但不幸的是他们并没有解决我的问题。
当我将新图片粘贴到我的可绘制文件夹中时,我的 R.java 就消失了。
阅读此问题的答案后,我尝试删除之前粘贴的图标(同时启用自动构建),之后一切都很好。希望这会对某人有所帮助!

I have tried all the solutions above. But they unfortunately it did not solve my problem.
My R.java was gone as soon as I pasted a new picture to my drawable folder.
After reading the answers for this question I tried to delete the icon I have previously pasted (while Build Automatically Enabled) and everything was fine afetr. Hope this will help someone!

握住我的手 2024-08-24 14:30:04

我遇到了这个问题,我只需右键单击我的项目包并构建项目。 R 类立即出现在 packet gen 中。

I had that problem and I just right-clicked on packet of my project and Build Project. Instantly the class R appeared in packet gen.

叹沉浮 2024-08-24 14:30:04

万一其他人在谷歌搜索时发现这个线程,就像我一样:

我有一段时间似乎遇到了同样的问题,经过一番挖掘,我注意到 R.java 文件已正确构建,但仅被忽略 我的一个源文件。这个特定文件的问题是 Eclipse “有帮助地”在顶部添加了以下行:

import android.R;

这有效地隐藏了项目 R 内容。只要将其删除,一切都会恢复正常...

In case someone else finds this thread when Googling, like I did:

I had what appeared to be the same problem for a while, and after some digging I noticed that the R.java file got correctly built, but was ignored for only one of my source files. The problem for this particular file was that Eclipse had "helpfully" added the following line at the top:

import android.R;

That effectively hides the project R stuff. Just remove it and everything will be fine again...

聆听风音 2024-08-24 14:30:04

如果旧的 R 文件已被删除,您可以使用 Eclipse 的“从本地历史记录恢复”来恢复它。之后,您可能会看到是什么阻止 Eclipse 构建您的文件(我已经看到了比以前更多的错误,这确实有助于解决问题)。

You can use Eclipse's "Restore from local history" to restore your old R file if it has been deleted. After that you possibly see what keeps Eclipse from building your files (I've seen other errors than before which really helped fixing the problem).

夜夜流光相皎洁 2024-08-24 14:30:04

也许您可以转到项目属性并选择 Android 选项,然后定义项目的目标。一旦您为该项目应用所选的目标 API,它将自动生成 R.java。

May be you can go to project properties and select Android option and then define the target for the project. Once you apply the selected target API for this project, it will automatically generate R.java.

瑾兮 2024-08-24 14:30:04

主要是布局文件无效的问题。
打开“问题”视图并查找 XML 布局文件中的错误。修复它们,R 类将被重新创建。

就我而言,没有真正的错误。问题视图显示 xml 布局中存在错误。编辑器也显示了 xml 错误。只需使用带有“ctrl-f”的自动格式化程序重新格式化 xml,即可“修复”xml“错误”并重新创建 R 类。

Mostly a problem with an invalid layout file.
Open the 'Problems' view and look for erros in XML layout files. Fix them and the R class will be recreated.

In my case there wasn't a real error. The problem view showed an error in a xml layout. The editor showed the xml error too. Just reformatting the xml using the auto formatter with 'ctrl-f' 'fixed' the xml 'error' and the R class was recreated.

独享拥抱 2024-08-24 14:30:04

您只需从项目中删除 R.java 文件,假设您已启用自动构建,它将重新生成。如果您没有自动启用构建,只需转到项目->构建全部

You can just delete the R.java file from your project and it will regenerate assuming you have build automatically enabled. If you do not have build automatically enabled simply goto project->build all

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