问题
In View.java,
setId - "Sets the identifier for this view. The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number."
&
findViewById - "Look for a child view with the given id. If this view has the given id,
Why I could not use a negative number as the id of the view? I commented out the
public final View findViewById(int id) {
// The original android check at here
// if (id < 0) {
// is commented out to test if system could run when Ids are negative numbers
if (id == NO_ID) {
return null;
}
return findViewTraversal(id);
}
The system seems to be working fine.
回答1:
It most likely is just a design decision. Negative identifiers are commonly used to notify that there's something wrong with the underlying entity, thus using negative id's is often frowned upon.
来源:https://stackoverflow.com/questions/8849121/android-why-should-ids-of-views-should-be-positive-numbers