R cannot be resolved - Android error

今天再次遇到这个问题,纠结了十多分钟。哦,原来Marko提到过,解决了之后才又重新看到。上次没做记录,补上!还有很多不同的原因造成这同一个问题,也有不同的解决方法,自己也遇到过好几种了。浏览了外国网站的一些回答,最后是官方的解释,不解释(翻译)。





Eclipse’sOrganize Imports tool can sometimes lead to hard-to-find problems. For example,if your project doesn’t have R.java generated ( which might happen becausethere’s an earlier problem with one of the XML resources ), then OrganizeImports will import the android.R class. This other R class is part of theAndroid framework and has the same name as your local R class, making it hardto notice. So, if you have many compilation errors around your references to Rresources, check that android.R is not imported.——《LearningAndroid》Marko Gargenta

Eclipse的导入(shift+ctrl+o)工具有时候会带来很难发现的问题。 比如, 如果你的工程没有生成R.java(当XML文件的一个资源先前存在问题的时候这种情况就会发生),导入工具就会导入android.R这个类。这另外一个R类是Android框架的一部分,和你本地的R这个类有相同的名字,这使得你很难注意到它们的差异。所以,如果你对R里面资源的引用存在很多编译错误的话,检查确保Eclipse没有导入android.R。


1. shift+ctrl+o惹的祸,导入了android.R,手工去掉。


2. XML布局文件存在错误,阻止Eclipse对它的编译,所以找不到,改正布局文件的错误。


3. R 是自动生成的,保存了一些常量用来标识你的资源。(R is an automatically generated classthat holds the constants used to identify your resources.)如果它没有自动生成,关闭后重新打开你的Project,或着新建一个Project。


4. 可能是没有选择自动编译,Project > Build all("BuildAutomatically")[by Josef]


5. 在Linux下可能是对Android Development Tools(ADT)没有权限,所以chmod吧。


6. R.java没有生成,最可能是创建工程时没有选择Build Target。


7. 当你不是在root namespace下新建Activity时,这种情况可能发生,在新Activity里导入R.java的正确路径,例如com.example.myapp是root namespace,那么import com.example.myapp.R;


8. XML文件名必须全部小写。


9. 1.5的API可能不认res/drawable-hdpi, res/drawable-mdpi, res/drawable-ldpi,只认res/drawable。


10. 未保存正在编辑的XML,ctrl+s。


11. R.java is afile that the android eclipse plugins creates while bulding your application.R.java is created under the "gen" directory. This file is generatedfrom the information in the "res" directory. If you run select"Project" -> "Clean..." on the eclipse menu it willremove and then regenerate the R.java file.

    The problem "R cannot beresolved" happens when you change your package name in theAndroidManifest.xml file. It uses your android package name to create asubdirectory under the "gen" directory where it stores the R.javafile.

    Eclipse may have problems executing cleanbecause it is confused about where the R.java file is when you have changed theandroid package name, you can either rename the subdirectory under gen to matchyour new package name, or you can change your package name back to the oldname, do the clean and then change the package name to the new name you want.This works best if you stop eclipse from trying to build while you are changingthe package name. Under the "Project" menu uncheck the option to"Build Automatically" and also when the "Clean..." dialogasks if it should "Start a build immediately" uncheck the box so itdoesn't try to build while you are changing the package name. After you havechanged the name you can turn "Build Automatically" back on again.

    Note that if your AndroidManifest.xml filepackage name does not match your java package name eclipse will end upautomatically adding an "import .R;" line in all your .java filesthat have any references to R. If you change your AndroidManifest.xml packagename, sometimes eclipse does not update all of these added imports. If thathappens use the eclipse refactoring (Keys ALT + SHIFT + R) to change the importstatement in one of your java files to your new AndroidManifest.xml packagename. It is best to do this while you have disabled "BuildAutomatically"—— the Google Groups "AndroidBeginners" group



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值