4

We are new to Android and we are trying to develop a new application. We installed all the basic setup of Android. We tried to run the Android Sample projects in Eclipse. But while to compile the following lines and a few other lines that uses the variable 'R' throws up an error.

setContentView(R.layout.activity_main);

'R cannot be resolved to a variable'. I do not understand what is causing the error. Anyone help me out in this issue.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
Manoj Kumar
  • 135
  • 1
  • 3
  • 13

10 Answers10

7

check that the import for R class is not

import com.android.R;

and you should make it

import yourpackagename.R;

then recompile

confucius
  • 13,127
  • 10
  • 47
  • 66
3

Please try to clean the project, and try compiling again.

If not solved, refer to R cannot be resolved - Android error

Community
  • 1
  • 1
Jason Kuang
  • 261
  • 2
  • 8
2

If you have 64 bit Linux platform without ia32 libraries, then this problem could also occur. It took me quite some time to pin this one down, seeing that the problem is not clearly linked with the inability to execute 32 bit code.

NTwoO
  • 75
  • 6
1

Just make the import part import com.android.R.layout instead of com.android.R

Alex Wiese
  • 8,142
  • 6
  • 42
  • 71
Emre
  • 1
  • 2
1

In my case i forgot to re-install the SDK. When you re-install your operating system, you should re-extract (re-install) your SDK Zip file.

0

Check if your resorce name (for example template.xml) has ONLY small caps letters.

baron_bartek
  • 1,073
  • 2
  • 20
  • 39
0

import com.packagename.R* will worl

Dapper Dan
  • 932
  • 11
  • 23
0

Change import to

import yourpackagename.R;

If error is still there then

go to gen folder->packagename->R.java 

and remove error line some times it gave error on drawable files.

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
Umer
  • 1,566
  • 2
  • 20
  • 31
0

setContentView(R.layout.activity_main)

Change the SDK and delete the project and create new ... it will definitely work!

0

Also check that you have your android libraries mapped correctly in the settings for Eclipse (if you are using eclipse to develop).

Bryan
  • 3,629
  • 2
  • 28
  • 27