Android Projects

Android Development Books

Wednesday

Android Development - “R cannot be resolved to a variable”?

If you have found this blog, most likely your project is drowning in plethora of red squiggly lines.    Everything was just fine and then all of the sudden BAM! Utter chaos!   No worries, be happy, because I have the cure to this annoy disease, and the remedy gives those red squiggly lines a swift kick in the class.
First lets check out what a healthy (but bare) project looks like:



Notice that this project has the R.java file within the gen folder, this is what a healthy project looks like (don’t be jealous, I’ll get you caught up in no time).






Now lets just glance at what problem we are dealing with:




Those red xs and red squiggly lines are truly frustrating, but those problems stem from the key problem.  If you haven’t figured out what the key problem is yet, I highlighted it… and it’s in the title of this article… ok, ok, it’s the missing R.java file.

Now how do we fix this?   Well, it’s a lot easier than you think, but first what is causing the R.java file to be deleted?  ”The answer lays in the resource’s name” may sound like some words of wisdom, but, umm, it’s also where the answer lays?!?!  Having a resources name not meet the standard guidelines below will cause your R.java to commit suicide.
Basically, when adding resources to your project you want to keep that resource’s name cool, sleek, and lowercase (just like mybringback).  If you create a new XML layout, add a sweet mp3, add an image, or any other resource to your project, make sure you protect yourself from the future headaches, and keep the name simple and lowercased.   I know it is going to be hard to quit naming your XML layouts “My$UPeR$W33TL@yOUT!#@$.xml” cold turkey, but trust me, you won’t have terrible withdraws once you get use to naming them to something like “mysupersweetlayout.xml”
So, just to clarify, to fix this project to the right, you would have to change the name “Bubble.png” to “bubble.png”,   “Error.xml” to “error.xml”, and “whatsUpMyGangstas.mp3″ to “whatsupmygangstas.mp3″  Now, if you’re feeling like the world doesn’t want you to be able to express yourself fully and limiting your creativity to only lowercase letters, you can rename “whatsUpMyGangstas.mp3″ to something like “whats_up_my_gangsta5.mp3″.

So by now you, I hope you know your resource names have to be lowercase, but  you can also add any of the numbers 0 through 9 as well as the _ symbol.  Once you have changed all the names of your resources, just clean the project ( Project>Clean..), wait a few seconds, and your R.java file will come back home.
Getting the R.java file back should get rid of most of your errors, but if it didn’t, check all your class’ imports for the “import android.R” and if it is there, delete it and clean the project again… or you can just check out the next blog post here: http://www.mybringback.com/bringers/android/44/android-development-travis-cannot-be-resolved-or-is-not-a-field/
More Problem just Like it, From stackoverflow.com
Hope that helps!
Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments

Thanks for your comment

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

Related Posts Plugin for WordPress, Blogger...
© Google Android Lovers
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0