Android ‘R’ Class

Just putting this up here in case anyone else falls into the trap I just did.

If you’re writing an Android application, and add a new Activity, you might want to refer to a generated class (from XML) using the ‘R’ class.

In Eclipse, it automagicly imports “android.R” as soon as you use it to attempt to access R.layout for a setContentView( R.layout.something ) call – this then breaks the class!

This is because the class is included as part of your project, but can be accessed via static calls from the Android API, causing a fun name conflict.

Simple workaround – remove the import line! Then your local copy will be used over the static methods of the class described in the API, fixing the issue.

This Post Has Been Viewed 92 Times




2 Comments

You shouldn’t usually refer to android.R.id unless you’re using xml in which case you wouldnt be able to import it anyway, I’ve never referred to android.R.id from code anyway. Eclipse is just too aggressive with all the importing I guess.

Just to clarify, MadrMan here means using the static ‘android.R.id’ from the class, and not that he doesn’t use R -anywhere- 😀