tomtom
Aspiring developer
Profile
Posts: 20
Reg: Jan 18, 2011
location unknow...
1,450
03/25/14 01:50 AM (10 years ago)

R cannot be resolved to a variable - Google API 2.2 level 8

Hello Everyone, I've been encountering a tricky issue with my eclipse recently and I'm stumped. My app always worked on the google api 2.2, but recently I opened it and since it has given me hundreds of "R cannot be resolved to a variable" errors. I tried searching the forums, and realized the importance of the API selected. After some experimenting, I found that if I set the API to google api 3.0 or higher the errors are resolved. Having said that, I have many users that have older phones that still run Android older than Honeycomb, and I'm concerned that publishing the app with the higher API levels will cause upgrade issues. Any ideas?
 
tomtom
Aspiring developer
Profile
Posts: 20
Reg: Jan 18, 2011
location unknow...
1,450
like
03/25/14 03:06 AM (10 years ago)
Hello Everyone, I've managed to resolve half the problem, and I think this could help others understand the nature of the issue. The root of the issue is the R.java file. This file exists in the gen/you_app/ folder. When using Eclipse, the file is re-created everytime the project is built or cleaned. The reason I had an issue was because I use Version-specific code. For example: if (android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.HONEYCOMB) { // call something for API Level 11+ } Now, when using Android APIs such as 2.2 (level 8), VERSION_CODES.HONEYCOMB doesn't resolve, given that honeycomb wasn't around when this API was released. As such, it throws an error, and for whatever reason, Eclipse decided NOT to generate the R.java file as a result, causing hundreds of "R cannot be resolved to a variable" errors. I tried to modify the code to act like this: if (android.os.Build.VERSION.SDK_INT >= 11) { // place API 11+ level code in here } and while the IF statement appears to work, the code within it is still being evaluated by Eclipse when the API is set lower. I'd appreciate assistance figuring out why this is happening.
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.