Search code examples
androidimageviewnumberformatexception

NumberFormatException in setTag(int key, Object tag) method


I'm trying to set a tag to an ImageView. But I got this exception:

NumberFormatException:Invalid int: "TAGTEST"

this is my code:

public static final String FIRST_TAG = "TAGTEST";
imageView.setTag(Integer.parseInt(FIRST_TAG.toString()), myIndex);

Why this code doesn't work???

Integer.parseInt(FIRST_TAG.toString())


Solution

  • Key has to be an Android ID.

    You can use a R.id.somethingfromyourviews

    Or

    Create an ID in an xml file, usually called ids.xml located at res\values

     <resources>
        <item type="id" value="integer" name="your_id_name"/>
     </resources>
    

    So you use it in code R.id.your_id_name