R cannot be resolved - Android error
I just downloaded and installed the new Android SDK. I wanted to create a simple application to test drive it.
The wizard created this code:
package eu.mauriziopz.gps;
import android.app.Activity;
import android.os.Bundle;
public class ggps extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
but Eclipse gives me the error
R cannot be resolved
on line
setContentView(R.layout.main);
Why?
PS: I do have an XML file named main.xml
under res/layout/
.
2Answer
After tracking down this problem as well, I found this note in the Android documentation:
http://source.android.com/source/using-eclipse.html
*Note: Eclipse sometimes likes to add an "import android.R" statement at the top of your files that use resources, especially when you ask Eclipse to sort or otherwise manage imports. This will cause your make to break. Look out for these erroneous import statements and delete them.*
While going through the Android sample tutorials, I would often use the Ctrl + Shift + O command to "Organize Imports" and generate any missing import statements. Sometimes this would generate the incorrect import statement which would hide the R.java
class that is automatically generated when you build.
- answered 8 years ago
- Sunny Solu
Whenever you get
R cannot be resolved
then check for the /res
directory and there must be some file that have some error in it and that is preventing the application from being built. For example, it may be a layout file or it may be due to some missing resource is, but you already defined it in the XML file.
If you have any additional, even unused (!) or unreferenced (!) images in a folder like res/drawables-mdpi which do not comply to the file naming conventions (may contain only [a-z0-9_.]), the R.java class might not generate, causing the chain of events all the other posts referred to. Hope it helps!
- answered 8 years ago
- Gul Hafiz
Your Answer