1
public class MainActivity extends AppCompatActivity {
    public void fade(View view){

        Log.i("INFO", "Image pressed");

        ImageView bart = (ImageView) findViewById(R.id.bart);

        ImageView.animate().alpha(0).setDuration(2000);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

here the problem is something with animate please help and thanks in advance.

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • 1
    `ImageView.animate()` is not possible, you need an instance of `ImageView` for that to work. – deHaar Jul 18 '19 at 08:52
  • 1
    Possible duplicate of [What is the reason behind "non-static method cannot be referenced from a static context"?](https://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static) – Markus Kauppinen Jul 18 '19 at 09:24
  • @MarkusKauppinen How is this duplicate? The context of the post and the link you provided is different. – Shahadat Hossain Jul 18 '19 at 10:15
  • It's the exactly same problem. It's irrelevant what the class and method are as long as the basic concept is wrong. The non-static method `animate()` is here called from a static context i.e. by using the class `ImageView` instead of an instantiated object of it. So, the OP is trying to reference a non-static method from a static context. (It doesn't matter is it and apple or a pear that you are trying to stuff up to your nose when the actual problem is that fruits are not supposed to go up to your nose but into your mouth instead.) – Markus Kauppinen Jul 18 '19 at 10:39
  • Ah, okay. The code in that suggested duplicate target question is really quite different. I went more by the title and the answers that were explaining the basic concept. There could be a better older question somewhere. – Markus Kauppinen Jul 18 '19 at 10:43
  • uhh ,sorry i fixed that 5th line was incorrect i changed Imageview.animate to bart .animate and it worked thanks for your answers btw. – aayush upadhyay Jul 18 '19 at 15:05

3 Answers3

4
ImageView bart = (ImageView) findViewById(R.id.bart);
ImageView.animate().alpha(0).setDuration(2000);

There's your problem, change that to:

ImageView bart = (ImageView) findViewById(R.id.bart);
bart.animate().alpha(0).setDuration(2000);

Seeing as animate is not static, it needs an instance of the class to be executed, since it's specific for each instance.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
0

You call the animate(); method on the ImageView class itself and not the instance. This will only work when the method is declared as a static method.

From the question and the very straight forward error message, I can see that you're not familiar with the static keyword. So I'll explain it to you...

A static method can be very usefull in some cases and knowing what it is can help you in the future.

An example would be the decode(); method from the class Long. It decodes a String into a Long(how it does it is unimportant). The method isn't in the String class, because encapsulation is important in OOP and if you had 20 different decode(); methods for Int, Long, Double... that would create a big chaos in the String class. That's why we put it in the Long class itself in the first place and not in the String class.

If the method wasn't a static you would need to write it like that:

String string = "123";
Long number = new Long();
number.decode(string);

With the static it looks like this:

String string = "123";
Long number = new Long();
number = Long.decode(string);

As you can see, using static was very usefull back then, because you are able to see that you use the decode method from Long and not from Int, Double or anything like that. If you had 100 text rows between Long number = new Long(); and number.decode(string);, it wouldn't be very easy to find out from which class the method was and with that which result you'll get.

But this isn't a problem anymore. With a compiler like eclipse, you can see from which class a method comes from, without the static keyword. The only point why java classes still use it is:

It works and if they changed it, many programs wouldn't work anymore.

Now to your question. You treat the animate(); method like a static method, which it isn't and that makes sense. You only want to animate the specific instance of a class, in your case bart. That means(like Stultuske already said) you need to replace the class with an instance of the class in front of .animate(), in this case bart.

You need to replace this:

ImageView bart = (ImageView) findViewById(R.id.bart);
ImageView.animate().alpha(0).setDuration(2000);

with this:

ImageView bart = (ImageView) findViewById(R.id.bart);
bart.animate().alpha(0).setDuration(2000);

You create an instance of ImageView called bart and after that, you want to animate the created instance with [instance].animate()... not the class itself.

Fabian M.
  • 37
  • 6
0
  • Option 1

Replace this:

ImageView bart = (ImageView) findViewById(R.id.bart);
ImageView.animate().alpha(0).setDuration(2000);

With:

(ImageView) findViewById(R.id.bart).animate().alpha(0).setDuration(2000);
  • Option 2 (Answered by @Stultuske & @Fabian M)

Replace this:

ImageView bart = (ImageView) findViewById(R.id.bart);
ImageView.animate().alpha(0).setDuration(2000);

With:

ImageView bart = (ImageView) findViewById(R.id.bart);
bart.animate().alpha(0).setDuration(2000);

Follow any one option that you like most.

Shahadat Hossain
  • 533
  • 7
  • 20