简体   繁体   English

项目清理后,R.java消失了

[英]R.java disappears after project clean

After I added some .png files into my Project Workspace drawable folder I refreshed my drawable folders in Eclipse and the newly added files showed up. 在我将一些.png文件添加到我的Project Workspace可绘制文件夹后,我在Eclipse中刷新了我的可绘制文件夹,并显示了新添加的文件。 But when I tried to access those resources using R.drawable.xyzimage, xyzimage although in the folder could not be resolved. 但是当我尝试使用R.drawable.xyzimage访问这些资源时,xyzimage虽然在文件夹中无法解析。

So I did a Project Clean and guess what after this clean R.java is totally gone and all my classes accessing resources using R.java is showing all sorts of error and Eclipse won't let me run the code anymore. 所以我做了一个Project Clean并猜测在这个干净的R.java完全消失之后,我使用R.java访问资源的所有类都显示了各种错误,Eclipse不再允许我运行代码。

I have backup of my source code but I want to explore if R.java can be regenerated and how? 我有源代码备份,但我想探索是否可以重新生成R.java以及如何重新生成?

Thanks for all the help. 谢谢你的帮助。

Try Project->Clean in Eclipse. 在Eclipse中尝试Project-> Clean。 In my limited experience if R.java is not being automatically created then there is an error somewhere in your xml. 在我有限的经验中,如果没有自动创建R.java,那么你的xml中就会出现错误。 Triple check everything. 三重检查一切。

You have to build the project to be able to use the newly added resources. 您必须构建项目才能使用新添加的资源。

Cleaning a project removes all auto-generated files. 清理项目会删除所有自动生成的文件。 Building the project it should automatically create them. 构建项目应该自动创建它们。 When you Clean a project, there's an option to start a build immediately after clean up. 清理项目时,可以选择在清理后立即启动构建。

Here's a few things you can try (did the same procedure after manually deleting the " gen " directory. 这里有一些你可以试试的东西(手动删除“ gen ”目录后做了同样的程序。

  • After you have deleted the gen directory, go to Project > Clean ... 删除gen目录后,转到Project> Clean ...
  • You should have errors indicating that R cannot be resolved to a variable. 您应该有错误表明R无法解析为变量。 Right click on your project from the Package Explorer and select Build Project. 右键单击Package Explorer中的项目,然后选择Build Project。 Be sure Build Automatically option is turned off (uncheck in Project > Build Automatically). 确保关闭“自动构建”选项(在“项目”>“自动构建”中取消选中)。
  • Errors regarding R should have now disappeared. 关于R的错误现在应该已经消失了。 Now, perform a Project Clean once again. 现在,再次执行Project Clean。 All errors should be gone. 所有错误都应该消失。

Let me know if this works for you. 如果这对您有用,请告诉我。

Here is what the problem was. 这就是问题所在。 I added those new png images to the project and android has rules as to what characters are allowed in the names of these images. 我将这些新的png图像添加到项目中,并且android具有关于这些图像的名称中允许哪些字符的规则。 Once I changed the uppercase letters in the image name, it was back on track again and Build Automatically generated the R.java file and all errors were gone. 一旦我更改了图像名称中的大写字母,它就再次回到正轨并且自动生成了R.java文件并且所有错误都消失了。 Eclipse console is the place where I found all the errors related to my resources. Eclipse控制台是我找到与我的资源相关的所有错误的地方。

Thanks Guys....I tried your solutions but my error was something specific which was due to my image naming convention so this is the way I fixed it. 谢谢大家....我尝试了你的解决方案,但我的错误是特定的,这是由于我的图像命名约定所以这是我修复它的方式。

I would agree this is usually a XML problem but moreover that it is something within your res folder that is the problem and may be highlighted with a red cross after the clean attempt. 我同意这通常是一个XML问题,而且它是你的res文件夹中的问题,并且可以在干净尝试后用红叉突出显示。

I have just had this problem and it was because I had created my own folder called 'data' within res folder. 我刚刚遇到这个问题,因为我在res文件夹中创建了自己的名为'data'的文件夹。 After the clean it had a little red cross against it. 清洁后,它有一个红色的十字架。 I removed this folder and R built again. 我删除了这个文件夹并重新构建了R.

Regards, Mark 问候,马克

Everytime you clean the project and it rebuilds, it imports 每次清理项目并重建时,都会导入

import.android.R import.android.R

so, delete this import and remove all errors from xml files. 因此,删除此导入并从xml文件中删除所有错误。 It will work. 它会工作。

Several things you should check in your project: 您应该在项目中检查几件事:

  • Check the Problems tab in your Eclipse IDE. 检查Eclipse IDE中的Problems选项卡。 Are you sure that you did not introduced building problems while making changes with drawables? 您确定在使用drawable进行更改时没有引入构建问题吗? To check that you need to check xml source of affected layout and see if something is marked red. 要检查是否需要检查受影响布局的xml源,并查看是否有东西标记为红色。

  • In File explorer go to the directory where your Android project is. 在File explorer中,转到Android项目所在的目录。 Check that you have file default.properties (.classpath, .project and AndroidManifest.xml should be there). 检查是否有default.properties文件(.classpath,.project和AndroidManifest.xml应该在那里)。 If default.properties is missing it may be easily replaced by correct version. 如果缺少default.properties,则可以很容易地用正确的版本替换它。

  • Which is the Android version you are working with? 您正在使用的Android版本是哪个? If that is 1.5 or lower be sure that in the res directory you have directory named drawable , not drawable-hdpi , drawable-mdpi and drawable-ldpi . 如果是1.5或更低,请确保在res目录中有一个名为drawable的目录,而不是drawable-hdpidrawable-mdpidrawable-ldpi Last three are valid for version 1.6 and higher. 最后三个版本适用于1.6及更高版本。

  • Right click on your project in Package explorer in Eclipse. 在Eclipse中的Package explorer中右键单击您的项目。 You will get a number of options. 你会得到很多选择。 Go to Android Tools-> Fix Project Properties so see if that helps. 转到Android工具 - >修复项目属性,看看是否有帮助。

I had an invalid jpg in my res/drawable folder. 我的res / drawable文件夹中有一个无效的jpg。 I removed the jpg and it worked. 我删除了jpg并且它有效。

I had this problem on my Mac too and I think it could be good to know for all Mac users how I managed to solve this error. 我在我的Mac上也遇到了这个问题,我认为对所有Mac用户来说,如何设法解决这个错误可能会很好。

I had a .DSStore file automatically imported to my project inside the res directory and when I removed it and rebuilt the project all went well. 我有一个.DSStore文件自动导入res目录中的项目,当我删除它并重建项目一切顺利。

This may be simple but check to make sure you don't use 这可能很简单但请检查以确保您不使用

import android.R; import android.R;

If you add an external resource file with capital letters for example "Hello.png or WelcomeSound.mp3 etc etc" like if you add upper case letters and clean your project you'd surely loose your R.java file. 如果你添加一个大写字母的外部资源文件,例如“Hello.png或WelcomeSound.mp3等”,就像你添加大写字母并清理项目一样,你肯定会丢失你的R.java文件。 So if you already lost it, either rename the file externally and then clean your project or else right click on the project Refractor->Rename and then rename your file with all letters in lower casing. 因此,如果您已经丢失了它,请在外部重命名该文件,然后清理项目,或者右键单击项目Refractor-> Rename,然后使用下部框中的所有字母重命名文件。 Next, delete the 'gen' folder in the project explorer and now right click and "Build Project". 接下来,删除项目资源管理器中的“gen”文件夹,然后右键单击“构建项目”。 If Build Project option is grayed out, go to Project-> Deselect "Build Automatically" option. 如果“构建项目”选项显示为灰色,请转到“项目” - >“取消选择”“自动构建”选项。

Regards, 问候,

Kshitij Thube Kshitij Thube

Check at the Console View If there in message Error, It will Help you to find out the source of the probleme. 控制台视图中检查如果在消息错误中,它将帮助您找出问题的来源。 I had i similar probleme and i find out that the probleme was from the pic name even if the name respect the Syntax so I change it to img1 and it worked fine. 我有类似的问题,我发现问题来自图片名称,即使名称尊重语法,所以我将其更改为img1,它工作正常。

This happens to me if I move my project to a new location. 如果我将项目移动到新位置,就会发生这种情况。 I have found that the answers above didn't fix my problem, but that creating a new project and importing the source did. 我发现上面的答案并没有解决我的问题,但创建一个新项目并导入源代码确实如此。

All my R.* files stopped working after I did a Project->clean in Eclipse. 在Eclipse中完成Project->clean之后,我的所有R.*文件都停止了工作。 The issue was that I backed up a XML file attached to one of my activities in the layout directory. 问题是我备份了一个附加到布局目录中我的一个活动的XML文件。 This causes errors in every activity of my project. 这会导致项目的每个活动出错。

After deleting the file I still had R errors in one of my Activities and it only was resolved after I tried importing different files and then deleting them. 删除文件后,我的一个Activities仍然有R错误,只有在我尝试导入不同的文件然后删除它们后才解决。 I actually made no change to the previous imports but for some reason deleting them all and re-pasting them fixed it. 我实际上没有改变以前的导入,但由于某种原因删除它们并重新粘贴它们修复它。

Make sure your png files have lowercase names. 确保您的png文件具有小写名称。 That is what kept throwing mine out, because I capitalized the first letter, or I used an unallowed non alpha numeric in the naming. 这就是扔掉我的东西,因为我把第一个字母大写了,或者我在命名中使用了一个不允许的非字母数字。 Check that before destroying any of your code. 在销毁任何代码之前检查一下。

只需检查您的图像文件夹(通常创建res>可绘制文件)并确保不要在.png文件上使用大写字母,每个类型必须是小写字母然后您可以再次找到您的R.java在gen文件夹下

i removed 'import android.R;' 我删除了'import android.R;' then commented out the offending lines, then did a build, ok 然后评论出有问题的线,然后做了一个构建,好吧

then i removed the comment and it fixed it 然后我删除了评论并修复了它

I had the same problem when I cleaned my project. 我清理项目时遇到了同样的问题。 I got crazy to solve it, I searched and searched but solved it in my way. 我疯了解决它,我搜索并搜索但是以我的方式解决了它。 First of all, delete all needless files in your /res folder . 首先, 删除/ res文件夹中的所有不必要的文件 I had some notes in it, I took them to another project that I don't work on it and build the project manually. 我有一些笔记,我把它们带到另一个我没有工作的项目,并手动构建项目。 It worked! 有效! Try this I'm sure it will help. 试试这个我相信它会有所帮助。

Good developing! 好发展!

Was there an XML file with the naming convention of "somthing.out.xml" generated? 是否有生成“somthing.out.xml”命名约定的XML文件? If so you should delete this file (an unwanted file of this name can cause major problems for your somthing.somthing.R file import). 如果是这样,您应该删除此文件(此名称的不需要的文件可能会导致您的somthing.somthing.R文件导入出现重大问题)。

In some versions of eclipse (I know eclipse javaEE does this for sure), running the project while viewing an XML file will create a file called filename.out.xml in your res->layout folder. 在某些版本的eclipse中(我知道eclipse javaEE肯定会这样做),在查看XML文件时运行项目将在res-> layout文件夹中创建一个名为filename.out.xml的文件。 You must delete this generated file if this has occurred otherwise you wont be able to get the java.R file to import. 如果发生这种情况,您必须删除此生成的文件,否则您将无法获取要导入的java.R文件。

尝试项目 - >自动检查构建

If you are using 64-bit Ubuntu, try to install the 32-bit libraries (ia32-libs) 如果您使用的是64位Ubuntu,请尝试安装32位库(ia32-libs)

$ sudo apt-get install ia32-libs 

it worked for me 它对我有用

This exact same thing happened to me. 这件事发生在我身上。 coincidentally I needed to update my adk that day, so i did so and after restarting eclipse it worked again. 巧合的是,那天我需要更新我的adk,所以我这样做了,重新启动eclipse之后再次工作了。 My recommendation would be to build the project and restart eclipse. 我的建议是构建项目并重启eclipse。 It is not due to any error in your code. 这不是由于您的代码中的任何错误。

I read and tried many of the above after simply doing a clean on one project. 在简单地对一个项目进行清理后,我阅读并尝试了上述许多内容。 Eclipse would not build anything afterwards. 之后Eclipse不会构建任何东西。

Fortunately I had last-nights backup of the workspace and my source is copied in from VS 2010. 幸运的是,我有最后一晚的工作区备份,我的源代码是从VS 2010复制而来的。

No more cleaning! 没有更多的清洁!

Delete your project from workspace witout source deleting and then import it into workspace. 从工作区删除项目,删除源删除,然后将其导入工作区。

RightClick on your Project -> Delete /Don't select "Delete project content on disk (cannot be undone)"/ -> Ok 右键单击您的项目 - >删除/不选择“删除磁盘上的项目内容(无法撤消)”/ - >确定

File -> Import -> General -> Existing Projects into Workspace -> Brouse -> /Look for your workspace(!) folder/ -> Ok -> /your cleaned project will be automatically checked/ -> Finish 文件 - >导入 - >常规 - >现有项目到工作区 - > Brouse - > /查找您的工作区(!)文件夹/ - >确定 - > /您清理的项目将自动检查/ - >完成

I had the dreaded missing R problem yesterday with Eclipse Juno after a routine clean. 在日常清洁之后,昨天Eclipse Juno遇到了可怕的R问题。 StackExchange is full of suggestions and extensive googling revealed only the usual suspects (XML naming conventions problems, etc) none of which seemed to apply directly, although it is certainly possible I missed something. StackExchange充满了建议和广泛的谷歌搜索显示只有通常的嫌疑人(XML命名约定问题等),似乎没有一个直接适用,虽然我肯定可能错过了一些东西。

The best summary comprehensive list of possible causes and fixes was here: 有关可能原因和修复的最佳摘要综合列表如下:

http://www.techrepublic.com/blog/software-engineer/a-comprehensive-troubleshooting-guide-for-androids-r-cannot-be-resolved-error/ http://www.techrepublic.com/blog/software-engineer/a-comprehensive-troubleshooting-guide-for-androids-r-cannot-be-resolved-error/

One useful tip there was to try to create and run a simple " Hello World " in my workspace. 一个有用的提示是尝试在我的工作区中创建并运行一个简单的“ Hello World ”。 If that does not run, the setup is corrupted. 如果没有运行,则安装程序已损坏。 My Hello World was just as full of errors as my custom app. 我的Hello World和我的自定义应用程序一样充满了错误。

The solution that worked for me (perhaps great overkill but nothing else worked) was to completely uninstall Android SDK and delete Eclipse (Juno), download Eclipse for Developers (Kepler) and reinstall Android ADT plugin and ADK and recreated my AVDs using the latest "compile with" version in options. 对我有用的解决方案(可能是极大的矫枉过正,但没有其他工作)是完全卸载Android SDK并删除Eclipse(Juno),下载Eclipse for Developers(Kepler)并重新安装Android ADT插件和ADK并使用最新版本重新创建我的AVD用“选项中的版本”编译。

So far so good, although I am recreating the app from scratch. 到目前为止这么好,虽然我从头开始重新创建应用程序。 I will be more cautious about using clean from here on. 从这里开始使用clean会更加谨慎。

我刚才遇到同样的问题我将无效的png文件重命名为* demo.1.png,作为demo.png *及其工作。

经过验证的成功方法,检查文件(AndroidManifest.xml)并删除其中的所有注释,并确保此文件中没有错误,我尝试过并与我合作。

尝试编辑项目中的一行代码,然后再次构建项目。

Another thing: never try to run an application with the texteditor opened with a xml file. 另一件事:永远不要尝试使用xml文件打开texteditor来运行应用程序。 That will create a wierd run configuration that can only be fixed by project clean-up and sometimes can make the xml file to vanish (happened to me twice already). 这将创建一个奇怪的运行配置,只能通过项目清理来修复,有时可以使xml文件消失(发生在我身上两次)。

Check your file in drawable folder ensure that all are resource file (image) remove unwanted file Like thumb.db . 检查您的文件在drawable文件夹中确保所有资源文件(图像)删除不需要的文件像thumb.db Remove those all files and check again 删除所有文件并再次检查

I have got the same problem after lots of cleaning and building project again and again. 经过大量的清洁和一次又一次的建筑项目,我遇到了同样的问题。 I found that on console there is this error: 我发现在控制台上有这个错误:

res/drawable-hdpi/Blue Blank.png: Invalid file name: must contain only [a-z0-9_.]

and this is not appearing as an error in xml. 并且这不会在xml中显示为错误。 I changed the name and save the file, clean project and build. 我更改了名称并保存文件,清理项目和构建。 Now it's working. 现在它正在运作。 My suggestion check your res folder and name of files you use in it and name only [a-z0-9_.] . 我的建议检查您的res文件夹和您在其中使用的文件的名称,并仅命名[a-z0-9_.]

Delete related tag in xml, save xml, clean project, build project. 删除xml中的相关标记,保存xml,清理项目,构建项目。 Now again put that resource in xml and save! 现在再次将该资源放入xml并保存!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM