Possible Duplicate:
Scope of R.id values
Could someone please clarify the scope of resource id's in Android? Recently I was making a custom title bar which contained a RelativeLayout called "header". I was invoking this title bar in the onCreate() handlers of each of my Activities thusly:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_layout);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
One of my Activities had a TextView in its layout with the id of "header" and in that Activity's onCreate() I do a
TextView tv1 = (TextView)findViewById(R.id.header);
. . . which blew up at runtime with ClassCastException, apparently because it thought I was trying to cast a RelativeLayout to a TextView. Changing the name of the RelativeLayout in the title bar fixed it.
But I don't really understand this. Are Android resource id's global? If so why wouldn't this collision be caught at build-time?
Note that this question has been asked before on SO: Scope of R.id values . . . Where the OP said "as far as I can tell" R.id's are global but my question is not a duplicate because neither of my questions is really addressed in that one.