R cannot be resolved - Android error

2018-12-30 23:01发布

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/.

30条回答
步步皆殇っ
2楼-- · 2018-12-30 23:24

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.

查看更多
旧人旧事旧时光
3楼-- · 2018-12-30 23:26

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 a source.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.

查看更多
旧人旧事旧时光
4楼-- · 2018-12-30 23:27

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.

enter image description here

查看更多
十年一品温如言
5楼-- · 2018-12-30 23:30

my project have include a r.java.at the beginning ,R.layout.main work good.But,after adding some code it doesn't work,and the error is R.layout.main can't resolved.what's the problem?

Look at your imports. Chances are that the line:

import android.R;

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.

查看更多
泪湿衣
6楼-- · 2018-12-30 23:30

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.

查看更多
闭嘴吧你
7楼-- · 2018-12-30 23:31

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 the com.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:

import com.example.myapp.R;
查看更多
登录 后发表回答