I gave my ImageView with android:Tag="1" but when I try to find this view with ImageView.getTag(1); it shows the error: Non static method "getTag(1) cannot be referenced from a static context.
What can I do? How can I make a non-static Tag?
I gave my ImageView with android:Tag="1" but when I try to find this view with ImageView.getTag(1); it shows the error: Non static method "getTag(1) cannot be referenced from a static context.
What can I do? How can I make a non-static Tag?
ImageView
is a class. If you create an instance of ImageView
, say using
ImageView myImageView = new ImageView();
then you can then refer to getTag()
non-statically using myImageView.getTag()
.
If you insist on using ImageView.getTag()
then getTag()
and tag
should both be declared to be static. This would mean that there is only ever one value of tag
, at any one time, for all ImageView
instances.
That's just basic java.
In Android there is already a getTag()
, though not in ImageView
but in View
. ImageView
inherits from View
so you get it there anyway.
ImageView
is documented here:
http://developer.android.com/reference/android/widget/ImageView.html
View
is documented here:
http://developer.android.com/reference/android/view/View.html
android:Tag
is an XML attribute for View
and is documented here:
http://developer.android.com/reference/android/view/View.html#attr_android:tag
As I mentioned getTag()
exists on View
not ImageView
but since ImageView
inherits from View
all instances of ImageView
will have it.
You can see the getTag()
source code here:
And that conclusively shows that getTag()
is in fact not static
so should be referenced through instances and not literally as View.getTag()
.
Documenters sometimes tell you to refer to non-static methods in what is actually a static way. They assume you know not to take them literally and they aren't always right. In their defence they have no idea what you name your instances so they don't know what else to call it.
yourView.getTag()
would be more correct but after a while becomes annoying to look at.
You did say you were trying to "find this view". Until you have a reference to the view you're after getTag()
won't help you. You already know your tag is 1
. You don't need to be told that again. You need to find your way to the view that has tag 1
.
In that case look here: