Duplicate ID in XML in Android

Source: Internet
Author: User

Duplicate ID in XML in Android

Statement: Reprinted with the original address.


Today, when I was working on the project, I found that @ + id can be duplicated, and the program will not report an error. Depressed. What is going on? After Google and Baidu, I learned something about it.

Scenario 1: the same name in the same XML file

If the same XML file has the same name, the previous one is valid, and the next one is invalid, that is, the last one is null. For example:

    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_above="@+id/textView1"        android:layout_alignRight="@+id/textView1"        android:layout_marginBottom="58dp"        android:layout_marginRight="18dp"        android:text="Button1" />    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignRight="@+id/button"        android:layout_centerVertical="true"        android:text="Button2" />

The previous button is valid (Android: text = "button1"), and the last button is invalid.

Case 2: the same name in different XML files

If the same name exists in the same XML file, both of them are valid.

When the android project is getting bigger and bigger. More and more XML files cannot avoid using the same ID name for the same components in the two XML files, R. in the Java file, the ID declaration is in the class of the ID, that is, public static final class ID {}; when the same components in the two XML files, such as button, there may be many files with ID = "@ + ID/button". At first, I thought that duplicate IDs will be referenced in the Java class, causing program non-recognition.

Later, when an error is discovered by chance, you only need to setcontentview (R. layout. updatemyaddressbook); the IDs in the XML file and the XML file related to this XML file are not repeated, and the findviewbyid (R. id. button); the program does not recognize anything else.

This problem is easy to understand: In the Framework Design of Android, every control belongs to a control tree, and every control is managed and allocated by its parent control, the root control is a container control, and all child controls are constructed on the root control. In this way, a control domain of the Control tree is formed, duplicate names are not allowed in this control domain. If the control domain is exceeded, the IDs of these controls are invalid. That is to say, the sub-controls in the container control do not allow duplicate names, it doesn't matter if the two controls are not in the same container control.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.