When designing the view class, Android designs a Settag/gettag method in order to store some auxiliary information. This reminds me of the same tag in every control in WinForm design.
What I'm going to talk about today is my recent study of Settag's Hole in Android. In general, we only need to use the Settag method with a unique parameter. But sometimes we need to store multiple data, so at this point we need to use an overload with a key.
The document is described as: "The specified key should is a ID declared in the resources of the application to ensure it's unique (see the ID Resource type). Keys identified as belonging to the Android framework or not associated with any package would cause an ILLEGALARGUMENTEXCE Ptionto be thrown. "
This shows that the key must be unique, but if we use Java constants to define key (private static final int tag_id = 1;) then you will still encounter the following error:
java.lang.IllegalArgumentException: The key must be an application-specific resource id
The correct solution is:
Define this key constant in Res/values/strings.xml, as follows:
<resources> <item type="id" name="tag_first"></item> <item type="id" name="tag_second"></item> </resources>
Use the following:
imageView.setTag(R.id.tag_first, "Hello"); imageView.setTag(R.id.tag_second, "Success");
Key issues with the Android Settag method