Key issues with the Android Settag method

Source: Internet
Author: User

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

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.