32

This error occurs in setContentView line in this code snippet:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

I understand that in order for R.layout.main to be resolved, a file named res/layout/main.xml must be present.

It is present and valid (i.e. Eclipse marks it as perfect without any errors). So, what else could cause this error?

BTW, I already tried Project > Clean. The error persists.

Cristian
  • 198,401
  • 62
  • 356
  • 264
Android Eve
  • 14,864
  • 26
  • 71
  • 96

9 Answers9

140

Make sure you don't have this in your imports:

import android.R;

but:

import your.application.packagename.R;
Cristian
  • 198,401
  • 62
  • 356
  • 264
  • you are king. I replaced **import android.R;** with **import com.example.ch15_1_simple_tts_demo.R;** and that cleaned all errors but introduced a new one: "The declared package "" does not match the expected package package com.example.ch15_1_simple_tts_demo". Fortunately, Eclipse is like a bike with training wheels so it suggested adding a package declaration at the beginning of the .java file. So many fine details... You would think that in the 21st century programming for android would be different than writing assembly code for the 8088. – Android Eve Feb 28 '11 at 15:08
  • 11
    For me I only needed to delete the "import android.R;" – DonnaLea Sep 05 '11 at 04:36
  • 2
    That's because you were inside a class that belongs to the same package of your `R` class. – Cristian Sep 05 '11 at 13:30
  • I don't have any of these statements in my app. – Hafiz May 23 '12 at 10:00
  • If I add `import com.example.AndroidHello.R;` I get error "import ... cannot be resolved". What should I do ? – Buksy Nov 01 '12 at 22:46
  • In the example I said: `your.application.packagename.R`, but you have insead `your.application.packagename.YourClassName.R` which is wrong. – Cristian Nov 02 '12 at 16:25
10

Also try: add

import your.application.packagename.R;

and run: eclipse->project->clean..

The error should be gone.

giorgioca
  • 713
  • 1
  • 8
  • 21
2

If this is the problem you are encountering then check at the top of your code - you will see:

import android.R

Delete that line and and change it into

import (com.xx.yy)

Replace (com.xx.yy) with your actual package name for the class. This problem occurs mainly when you copy all the XML and Java code of another application and paste it into your new application.

This is an example of how I replaced the android.R packages within one of my own apps (a puzzle game):

package com.pir.PUZZLEGAME_NEW;

import com.pir.PUZZLEGAME_NEW.*;
import com.pir.puzzlegame_old.R;

Good luck!

Perception
  • 79,279
  • 19
  • 185
  • 195
2

Delete all your import statements from the class where you are getting this error message.

Then press ctrl+shift+o

Tisho
  • 8,320
  • 6
  • 44
  • 52
yogesh
  • 21
  • 1
1

I changed

   import android.R;

to

 import your.application.packagename.R;

And project ->clean still the error.

But when I Run the project the error automatically gone, may be Eclipse problem.

Haris
  • 13,645
  • 12
  • 90
  • 121
1

for me changing

import org.opencv.R;

to

import com.example.opencvtryagain.R;

solved it, where as my package name was com.example.opencvtryagain;

0

If you are using eclipse, then

  1. Delete all your import statements from the class where you are getting this error message.

  2. press CTRL + SHIFT + O --> OR select Source from menu and select Organize import. This will import all the necessary classes.

Note: Since you have a local R.class file, it will import your local file instead of the android.R file.

Hope this will help someone.

Joseph Selvaraj
  • 2,225
  • 1
  • 21
  • 21
0

I have removed the following line and it worked for me.

import android.R;

dev_android
  • 8,698
  • 22
  • 91
  • 148
0

Also, sometimes it still won't compile even after making that needed change; but if you go ahead and run the app, the errs will clear up.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862