Solution to key problem of Android settag method

When Android designs the view class, in order to store some auxiliary information, it designs a settag / gettag method. This reminds me that in WinForm design, each control also has a tag.

Today I want to talk about the settag pit I met recently when learning Android. In general, we only need to use the settag method with unique parameters. But sometimes we need to store multiple data, so we need to use overloading with key at this time.

The document is a description: "the specified key should be an ID declared in the resources of the application to ensure it is unique (see the ID resource type). Keys identified as being to the Android framework or not associated with any package will cause an illegalargumentexception to be thrown."

It is stated here that the uniqueness of the key must be guaranteed, but if we use Java constants to define the key (private static final int tag_id = 1;) In this way, you will still encounter the following errors:

java. lang.IllegalArgumentException: The key must be an application-specific resource id

The correct solution is:

In RES / values / strings The key constant is defined in XML as follows:

Use the following:

The above is the solution to the key problem of Android settag method. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>