Error: R cannot be resolved

Asked

Viewed 17,662 times

16

I am creating an Android project and am facing the following error in my IDE.

package br.com.app;
import android.R;

public class MinhaApp extends Activity {

     @Override
     public void onCreate (Bundle savedInstanceState) {
         super.onCreate (savedInstanceState);
         setContentView (R.layout.main);
     }
}

But my IDE makes the following mistake:

R cannot be resolved

on the line

setContentView (R.layout.main);

Why is this happening? The file maix.xml is in the folder res/layout correctly.

  • You did import android.R;, Deleting this line will solve your problem.

9 answers

31


Give Clean and then Build in your project.

  • 1

    Thanks! As this class is generated dynamically during the compilation, the android import. R should be removed. Optimizing Imports by the IDE also solved.

  • Doing this is really boring, someone should fix it.

10

You imported the R 'wrong' by mistake. Change the line of the import for:

import <pacote_do_projeto>.R

For example:

import br.com.app.R

If the project package is 'br.com.app'

5

This is a common mistake that can come from several different sources:

  • Error on one of the Xmls (even if it does not accuse);
  • Do not save before Clean/Build;
  • Any errors preventing automatic generation of this file;

I found a solution so far "definitive" here for the people where I work.

EDIT.: This form should be used as the last case, if none of the above has worked:

Since Eclipse naturally generates the file and something was blocking this procedure, I chose to do it manually. I went to the directory (Agenda/gen/br/com/contacts/UI) (for example), inside the project folder, created a copy of Buildconfig and edited its name for R. Then created a new project and copied the R code to the copy. Finally, in the project of the contacts agenda, I gave Save, Clean and Build All so that the settings were updated and the errors (generated by the lack of it) corrected. In this way, the new class R is adapted according to the requests of the created code.

I have not tested it in every possible way, but it has served everyone I have indicated. I do not know to what extent it is correct to carry out the procedures in this way, so I ask you to give me a feedback if it does not work.

  • I recommend editing your answer to include this feature, so future visitors with the same question can solve it.

  • Edited, by Tiago.

5

This happens often when you have just created a project, which seems to me to be your case, so you can see from the code.
So just relax and wait a little while.
It turns out that as soon as you create a new project, several processes are running at the same time (the Java compiler, the Android Development Tool generators, the other IDE plugins,...). So sometimes it takes a little while for the ADT to generate the R class.
If that doesn’t happen just follow the @iTSangar reply.

4

This can occur for several reasons, but the most common are:

  1. Newly Created Project. Give Clean and Build.
  2. Class order in Java Build Path is incorrect or wrong priority. Preferably leave the following: Android Dependencies, Android Private Libraries, the others
  3. Error in some xml (layout, drawable, etc.).

2

What is the R

When developing applications for android, the implemented features are defined by an external XML file. This guarantees the division between the presentation layer and the application layer, in the background the separation of the appearance logic.

Create an xml file that stores all information contained in the res folder of the project.

The Eclipse ADT plugin accesses the references of all resources through the R class, which is regenerated whenever a new resource is defined in the project or whenever the project is rebuilt (rebuild).

1

Every time that happens, the clean does it. But you’ve heard it over and over again. Deleting the R.java class, I had to close the eclipse and clean it, because the eclipse recreates.

1

Import is wrong. If there is any error in xmls, the R class is not generated. Read the warnings and you will know what is wrong with your project.

-3

Sometimes just close the project and open again solves. Otherwise delete, close and reopen also solves.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.