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/
.
Check the XML file names. Be sure that they're all in lowercase.
Also make sure that any image resource names are also all in LOWER CASE. I had a capital letter in the name of my jpg file, and it caused the R unresolved error right across my project.
Often times this is because of the MinSDK version number you supplied when creating the project. Example:
If you want 2.1 to be the minimum, Android 2.1 is actually API Level 7.
You can see what I am talking about when you browse the SDK you downloaded and installed. Navigate to the place you installed the SDK to (
C:\android-sdk-windows
for example) and open the folder named "platforms". You will see something like "android-7" listed as a folder, and if you open that there is asource.properties
file that, when opened with a text editor, will show you the corresponding platform version.When you create a project, and you must select a "Build Target" API, the last column in that list named "API Level" shows the number you are looking for when populating the MinSDK setting.
This is probably one of the most common mistakes that results in the
R.java
file not being created under Project > gen > packagename > R.java.First check is there any error in any xml layout or not, if then resolve it first.
Otherwise remove junit dependency from project and rebuild the project.
Look at your imports. Chances are that the line:
will be there. If that's the case, remove it, so that your project will resolve R not with the default Android Resources class, but with the one auto-generated from your /res/ folder.
R is an automatically generated class that holds the constants used to identify your resources. If you don't have an R.java file (it would be gen/eu.mauriziopz.gps/R.java in Eclipse with the 1.5 SDK) I would recommend closing and reopening your project or going to Project > Build all (and selecting "Build Automatically" while there as recommended by Josef). If that doesn't work than try making a new project, if the problem is recreated than post here again and we'll go into more detail.
This error can also be caused by adding an activity to a namespace that is different to the root namespace for your package.
For example, if
com.example.myapp
is the root namespace for your package, you can then add an activity to thecom.example.myapp.activities
namespace.This will produce the "R cannot be resolved" error.
To fix the import the R in the default namespace in your activity should be: