Android: why should IDs of views should be positive numbers?

假如想象 提交于 2019-12-24 03:52:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!