Android - 如何重新生成 R 类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2041083/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:19:08  来源:igfitidea点击:

Android - How to regenerate R class?

androidandroid-resources

提问by zorglub76

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

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

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

我已经将一个项目导入到我的 Eclipse IDE 中,但是由于 R 文件不是自动生成的,所以它给了我一个错误。

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

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

采纳答案by zorglub76

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

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

Because of this R class was not able to generate.

由于这个 R 类无法生成。

回答by Goufalite

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.

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

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

Project -> Clean,然后选择构建项目的选项。

回答by Muz

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.Rin your activity, remove it.
  • Run Project -> Clean. This will delete and regenerate R and BuildConfig.
  • Make sure Project -> Build Automaticallyis ticked.
  • Waita 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.
  • 确保R.链接到的任何内容都没有损坏。如果损坏,ADK 可能无法重新生成 R。修复 XML 文件中的所有错误
  • 如果您以某种方式击中并import android.R在您的活动中创建了某些内容,请将其删除
  • 运行项目 -> 清理。这将删除并重新生成 R 和 BuildConfig。
  • 确保Project -> Build Automatically被勾选。
  • 等待几秒钟让错误消失。
  • 如果它不起作用,请删除 /gen/ 文件夹中的所有内容
  • 如果它仍然不起作用,请尝试右键单击您的项目 -> Android 工具 -> 修复项目属性

What to do if R doesn't regenerate

如果 R 不再生怎么办

This usually happens when you have a broken xml file.

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

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

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

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

回答by John Klippenstein

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

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

  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).
  1. 如果一个 .xml 文件引用另一个不存在的文件,这是一个错误
  2. 在文件名中包含大写字母是错误的(为什么禁止,我喜欢驼峰式大小写)。

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

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

回答by Muhannad A.Alhariri

To generate the R file again:

再次生成 R 文件:

  1. Modify something in layout->activity_main.xml
  2. Save the file
  3. Project->Clean...
  1. 修改 layout->activity_main.xml 中的内容
  2. 保存文件
  3. 项目->清洁...

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

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

回答by Erich Douglass

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

您也可以右键单击project -> Android Tools -> Fix Project Properties. 这应该会导致重新生成 R.java 类。

回答by InsaurraldeAP

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

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

回答by Charles

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" :-)

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

回答by Temperage

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

为了重新生成 R.class,您将不得不尝试多种技术。

  • 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
  • 从 Java 源文件中删除任何 R 导入: import R.android
  • 确保通过转到“窗口”->“显示视图”->“问题”来打开“问题”面板
  • 如果您的 xml 资源中确实存在一些错误,则需要更正这些错误。就我而言,我必须更改我的 id 属性以包含“+”号,如下所示:android:id="@+id/grp_txt"
  • 现在继续做项目->清洁
  • 最后做项目->关闭,项目->打开

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

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

回答by Corona

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

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

recreate your R

重新创建你的 R

My Case:

我的情况:

  • Broken xml caused bug in R. (because of ?, ?, ü, letters in id)
  • clean project deleted my R.
  • 损坏的 xml 导致 R 中的错​​误。(因为 ?, ?, ü, id 中的字母)
  • 干净的项目删除了我的 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
  • 从本地历史重新创建 R
  • 在我的类中检查错误的 R 导入(如导入 android.R)并删除它们
  • 修复了该 xml 文件并将其内容保存在外部 txt 文件中
  • 从我的项目中完全删除了 xml。
  • 运行我的项目一次
  • 将固定的 xml 放回原处

and...

和...

  • gave my girlfriend a big hug without a reason after freaking out like a Hulk.
  • 在像绿巨人一样吓坏了之后,无缘无故地给了我的女朋友一个大大的拥抱。