-1

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?

antisepticum
  • 11
  • 1
  • 5
  • What's your actual code? What language? What framework? Your problem is self-describing (you are calling a static method from a non-statc method) but that doesn't tell us how to solve it. – Jeroen Mostert Feb 22 '15 at 12:51
  • @JeroenMostert sry, its Android Studio - Java – antisepticum Feb 22 '15 at 12:53
  • The method must be defined as static, obviously. Or you must call it from a non-static context, i.e. `ImageView view = new ImageView(); ... view.getTag(1)` – Andrei Nikolaenko Feb 22 '15 at 12:53

1 Answers1

0

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:

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1/android/view/View.java#View.getTag%28%29

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:

Find all views with tag?

Community
  • 1
  • 1
candied_orange
  • 7,036
  • 2
  • 28
  • 62