【Error Resolve】main cannot be resolved or is not a field

时间:12-08-17 栏目:安卓技术篇 作者:魔豆先生 评论:0 点击: 2,661 次

Link all references for a local rename (does not change  references in other files)

main cannot be resolved or is not a field

刚遇到这个纠结的问题,代码都没错,R.layout.main总是在layout上游错误提示波浪线。在网上查了好多都是说clean一下工程 然后build工程就可以了,或者fix project properties 但是还是解决不了...

原因可能是添加.xml文件或者资源文件时,系统自动添加了import android.R  ;  android.R是系统提供的资源,R是应用程序的资源。

这时候只要删除 import android.R;这条语句就可以了。




Look at your import statements at the top. If you are saying import android.R, then there that is a problem. It might not be the only one as these 'R' errors can be tricky, but it would definitely definitely at least part of the problem.

If that doesn't fix it, make sure your eclipse plugin(ADT) and your android SDK are fully up to date, remove the project from the emulator/phone by manually deleting it from the OS, and clean the project (Launch Eclipse->Project->Clean...). Sounds silly to make sure your stuff is fully up to date, but the earlier versions of the ADT and SDK has a lot of annoying bugs related to the R files that have since been cleared up.

Just FYI, the stuff that shows up in the R class is generated from the stuff in your project res (aka resources) folder. The R class allows you to reference a resource (such as an image or a string) without having to do file operations all over the place. It does other stuff too, but that's for another answer. Android OS uses a similar scheme - it has a resources folder and the class android.R is the way to access stuff in the android resources folder. The problem arises when in a single class you are using both your own resources, and standard android resources. Normally you can say import at the top, and then reference a class just using the last bit of the name (for example, import java.util.Listallows you to just write List in your class and the compiler knows you mean java.util.List). When you need to use two classes that are named the same thing, as is the case with the auto-generated R class, then you can import one of them and you have to fully qualify the other one whenever you want to mean it. Typically I import the R file for my project, and then just say android.R.whatever when I want an android resource.

Also, to reiterate Andy, don't modify the R file automatically. That's not how it's meant to be used.

声明: 本文由( 魔豆先生 )原创编译,转载请保留链接: 【Error Resolve】main cannot be resolved or is not a field

【Error Resolve】main cannot be resolved or is not a field:等您坐沙发呢!