Domanda

When adding a new View in a layout xml, I noticed that my build was failing when using the id @+id/package with the error message error: invalid symbol: 'package'. I then started experimenting and noticed that Java keywords such as public, void, int, etc. cause this error when aapt is running.

Why does this error occur?

È stato utile?

Soluzione

The resource compiler turns your resource ids into fields of the class R.id (which you can see by looking in the R.java file that is automatically generated when the resources are compiled). Since it is illegal to use a Java keyword as a class field name, it is illegal to use a Java keyword as the name of a resource (be it an id, a layout, or anything else).

Note that for the same reason you cannot use a number as a resource name (e.g., you can't have a graphics file named "3.png"). All resource names must be valid Java identifiers.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top