1

I am creating an Android project and I 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 gives the following error :

R cannot be resolved

on the line

setContentView (R.layout.main);

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

Mauker
  • 11,237
  • 7
  • 58
  • 76

4 Answers4

2

Remove this import:

import android.R;

It seems that your Eclipse tried to incorrectly auto import the R class from the wrong location. Removing that line should solve the problem.

If it doesn't, also try to add this import:

import br.com.app.R;

If that doesn't solve the issue, try to clean and rebuild your project after doing the steps above.

Note: Don't use Eclipse, it's not officially supported anymore as you can see on this link. Switch to Android Studio instead, you can download it here.

Mauker
  • 11,237
  • 7
  • 58
  • 76
1

Just you go to Android studio Build option and click on Rebuild project. It will definitely work.

Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
Gundu Bandgar
  • 2,593
  • 1
  • 17
  • 21
0

Check your all xmls for error, if there is an error in any xml of your project R(br.com.app.R) won't be generated and you will get the same error.

After removing errors remove import android.R; and put import br.com.app.R

Shivam
  • 492
  • 5
  • 18
0

R cannot be resolved issue many reasons.

Try this.

1) The first thing to try is to clean the project so that the your IDE will automatically attempt to rebuild the R.java file.

2) If this doesn’t work, the next thing to check is the top of your Activity or whatever class is referencing the R resources. Check for the following line: import android.R; if found then Remove this import. It seems that your IDE tried to incorrectly auto import the R class from the wrong location. Removing line should solve the problem.

3) If it doesn't work, also try to add this import: import br.com.app.R;

Pravin Fofariya
  • 346
  • 2
  • 11