Solution to key problem of Android Settag method _android

Source: Internet
Author: User
Tags constant definition

When designing a view class, Android designs a Settag/gettag method to store some auxiliary information. It reminds me that there is also a tag in each control in the WinForm design.

Today I'm going to talk about the settag I've been learning about Android. In general, we only need to use the Settag method with unique parameters. But sometimes we need to store multiple data, so this time we need to use the overload with the key.

The document is a description: "The specified key should be a ID declared in the" the application to ensure it is unique (the ID Resource type). Keys identified as belonging to the Android framework or do associated with any package'll cause an ILLEGALARGUMENTEXCE Ptionto be thrown. "

This shows that the key must be guaranteed to be unique, but if we use the Java constant definition key (private static final int tag_id = 1;) You will still encounter the following error:

Java.lang.IllegalArgumentException:The key must be 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");

The above is the solution to the key problem of the Android Settag method, thank you all for the support of this site!

Related Article

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.