If you develop an app independently, you may not encounter duplicate attributes of multiple custom classes. However, if it is developed by a team, you may encounter this problem. The two custom classes A and B use the same name to define attributes. In this case, the system reports a warning, attribute "XXX" has already been defined. A and B don't want to modify their own names. This is a headache.
For example, if you define an above attrs. xml file under the values folder, eclipse will report the error "attribute" icon "has already been defined. In preferenceheader, preference defines two identical attributes.
<? xml version = "1.0" encoding = "utf-8" ?>< resources > < declare-styleable name= "PreferenceHeader" > <!-- Identifier value for the header. --> < attr name= "id" format = "integer"/> < attr name= "icon" format = "integer" /> <!-- The fragment that is displayed when the user selects this item. --> </declare-styleable > < declare-styleable name= "Preference" > < attr name= "icon" format = "integer" /> <!-- The key to store the Preference value. --> < attr name= "key" format = "string" /> </declare-styleable ></ resources >
Solution: 1. Declare attributes in the XML file first. 2. Then reference declared attributes in the attribute set. The preceding file is modified as follows:
<? xml version = "1.0" encoding = "utf-8" ?>< resources > < attr name = "icon" format = "integer" /> < declare-styleable name= "PreferenceHeader" > <!-- Identifier value for the header. --> < attr name= "id" format = "integer"/> < attr name= "icon" /> <!-- The fragment that is displayed when the user selects this item. --> </declare-styleable > < declare-styleable name= "Preference" > < attr name= "icon" /> <!-- The key to store the Preference value. --> < attr name= "key" format = "string" /> </declare-styleable ></ resources >
Duplicate ATTR attributes are defined for multiple classes: Attribute "XXX" has already been defined