-2

I'm working on a project modifying code from Blueterm, a Bluetooth Android application. Upon building the project, I get multiple errors saying "R cannot be resolved to a variable". I tried cleaning the project, as well as deleting the R.java file, with no success. I also made sure the package name was correct in the AndroidManifest.xml file.

I'm stuck right now, would really like any input on this.

apaderno
  • 28,547
  • 16
  • 75
  • 90
user1475744
  • 1
  • 1
  • 1

5 Answers5

2

Look at the file in question and see what package R is in. If you imported the code to your project, it may be pointing to R in the old package.

For example, I recently copied com.android.HttpAuthenticationDialog into a project. It references R expecting that it com.android is the app's package. Since the project is actually com.example, com.android.R does not exist.

I needed to either move the class into the com.example package or add import com.example.R; to the Java file.

This could also happen if you updated AndroidManifest.xml to use a different package. If you change from com.bluetooth to com.mybluetooth for example, the R class will move to com.mybluetooth.R. Any class that was expecting com.bluetooth.R to exist would have this error.

Brian Nickel
  • 26,890
  • 5
  • 80
  • 110
2

I just used copy and paste from a new project (R.Java) and it fixed everything. R Java was missing.

Noa
  • 31
  • 4
1

So even though I fixed R.java in many times. I kept having issues of all sorts. One by one I fixed them.

But I found out something strange;

In my SRC folder (for my project) the R.java folder was locked (this is in windows not in eclipse). It had a lock icon on it. I right clicked and took ownership of it. After cleaning and saving and waiting eventually eclipse fixed it and now the error is gone.

If you need to know how to take ownership of a file just search on google.

If you know how one of my files got locked up like this that would be great to know! Thanks!

Noa
  • 31
  • 4
0

There are a few reasons.

  1. Make sure your variable is indeed there. Remember "@+id/varName"
  2. Make sure that your not importing android.R.* instead of your project resources
  3. Make sure there aren't any errors in any other one of you XML files. If there is it can't create your R.java file at all and thus you should have a bunch of errors all over.
Frank Sposaro
  • 8,511
  • 4
  • 43
  • 64
  • 1 and 2 wouldn't apply. It's not that they can't get the resource ID. They can't get R at all. 3 is the most likely cause. – Brian Nickel Jun 22 '12 at 19:35
0

It's caused by updates on your android sdk manager. Open the sdk manager, and update the following 3 tools:

"android sdk tools", "android sdk platform-tools", "android sdk build-tools"

Restart eclipse, press "Ctrl+Shift+O"

Thats all, your project should build the resource