Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: View.getTag/setTag and IllegalArgumentException

Tags:

android

Yes, I know you need a unique resource id when calling the version of these functions that requires a key, but I cannot for the life of me figure out how to create a resource id that can be used in this way. A final variable in my class wont work and neither will a hard coded value. Both threw an IllegalArgumentException. So what gives, how do you use these methods?

Spara

like image 848
Sparafusile Avatar asked Mar 12 '10 18:03

Sparafusile


3 Answers

To create resource ID, which can be used identically to the ones you set within XML (with @+id), add

<resources>
    <item type="id" name="myId"/>
</resources>

in XML in res/values/.

like image 197
Dimitar Dimitrov Avatar answered Oct 04 '22 21:10

Dimitar Dimitrov


From the Javadoc:

The specified key should be an id declared in the resources of the application to ensure it is unique.

So you can't just make up values and place them in a local variable.

Every resource you create whether it's a string (R.string.*), or a layout (R.layout.*) or an individual View (R.id.*) can have an ID. This is something you must be doing already.

If you do need to store multiple objects against a single View, then you need to use the R.id variant as a key, like someView.setKey(R.id.my_key_1, someObject).

like image 33
Christopher Orr Avatar answered Oct 04 '22 22:10

Christopher Orr


You can use this code :

<resources xmlns:android="http://schemas.android.com/apk/res/android">
  <item name="TAG_ONLINE_ID" type="id"/>
</resources>
like image 28
Ahmad Aghazadeh Avatar answered Oct 04 '22 22:10

Ahmad Aghazadeh