0

I get the following warnings when I initialize an object of type animation.

(The warnings are added as comments)

Animation bottomUp = AnimationUtils.loadAnimation(
     android.content.Context, // warning: Expression expected 
     R.animator.bottom_up     // warning: Expected resource of type anim
);

Here is a picture

enter image description here

Here is the code summary:

   public class MainActivity extends AppCompatActivity {

        // Class variables go here

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

                // Set onclick listener 

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                    // Animation Code

                    Animation bottomUp = AnimationUtils.loadAnimation(
                            android.content.Context, // warning: Expression expected 
                            R.animator.bottom_up // warning: Expected resource of type 
                    );


                    ViewGroup hiddenPanel = (ViewGroup)findViewById(R.id.hidden_panel);
                    hiddenPanel.startAnimation(bottomUp);
                    hiddenPanel.setVisibility(View.VISIBLE);


                }
            });



        }

        // Other stuff
    }

This is log cat after I try to compile

enter image description here

Here is where I use the erroneous code

  public class MainActivity extends AppCompatActivity {

listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {

 @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            Animation bottomUp = AnimationUtils.loadAnimation(
                    android.content.Context,
                    R.animator.bottom_up
            );


            ViewGroup hiddenPanel = (ViewGroup)findViewById(R.id.hidden_panel);
            hiddenPanel.startAnimation(bottomUp);
            hiddenPanel.setVisibility(View.VISIBLE);

I cannot find the bug.

I created the correct folder and files. Here they are.

enter image description here

Here is where I got the animation code I am using.

bottom_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromYDelta="75%p" 
        android:toYDelta="0%p"
        android:fillAfter="true"
        android:duration="500"/>
</set>

bottom_down.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromYDelta="0%p" 
        android:toYDelta="100%p" 
        android:fillAfter="true"
        android:interpolator="@android:anim/linear_interpolator"
        android:duration="500" />
</set>

Java

Animation bottomUp = AnimationUtils.loadAnimation(getContext(),
            R.anim.bottom_up);
ViewGroup hiddenPanel = (ViewGroup)findViewById(R.id.hidden_panel);
hiddenPanel.startAnimation(bottomUp);
hiddenPanel.setVisibility(View.VISIBLE);

Tried to create an anim folder. Got this message.

enter image description here

Community
  • 1
  • 1
the_prole
  • 8,275
  • 16
  • 78
  • 163

2 Answers2

1

First argument should be Context object. Try "this" if You are using it in Activity.

Second warning is because You have Your animation in animator folder (so it is recognized as animator resource) and the second parameter in AnimationUtils.loadAnimation is annotated with @AnimRes annotation so this is just warning/suggestion that Your parameter should be resource of type anim. This is just lint warning not compilator.

This You can compile:

Animation bottomUp = AnimationUtils.loadAnimation(
 this, // if You are in Activity
 R.animator.bottom_up 
);

To load Animator try:

Animator animator = AnimatorInflater.loadAnimator(this, R.animator.bottom_up );

but this is not what You want here. Just move files to anim folder.

Full sample code:

package com.example.user.exampleapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Animation bottomUp = AnimationUtils.loadAnimation(this,
            R.anim.bottom_up);
        TextView hiddenPanel = (TextView) findViewById(R.id.textView);
        hiddenPanel.startAnimation(bottomUp);
    }
}
sswierczek
  • 810
  • 1
  • 12
  • 19
  • I already tried `this`. That just results in `required: 'android.content.Context'` Not sure I understand your second point, but the underlining is red, so it is an error, not just 'lint catching'. – the_prole Oct 01 '15 at 21:23
  • @the_prole trust me it is just lint ;) Could You show more code? Where are You using Your anim load? – sswierczek Oct 01 '15 at 21:27
  • Nope, it's a compile error. I have added pictures at the top, and a log cat output. Also, see the Java at the bottom for the rest of the code. – the_prole Oct 01 '15 at 21:42
  • 1. Move files to anim folder from animator. 2.Pass Context, not some weird android.content.Context stuff ;) – sswierczek Oct 01 '15 at 21:44
  • Are you sure about that? The `animator` folder was created by Android Studio when I created the animation files in the resources directory. How do I create an `anim` folder with animation resources? – the_prole Oct 01 '15 at 21:48
  • Yes, see sample code which is working. I used TextView instead of ViewGroup. BTW what is the hidden_panel in XML? – sswierczek Oct 01 '15 at 21:51
  • Stupid question: How do I created anim folder with resources? It is more complicated than it sounds. – the_prole Oct 01 '15 at 21:55
  • Just create folder next to "animator" in "res" You can do it in Android Studio or file manager. For "this" If You are have this code in some inner class like Runnable it will not work as "this" will be inner class reference not Activity so because of that it is telling that You have to pass Context. It will be clear if You show current place of that code. – sswierczek Oct 01 '15 at 21:59
  • See the last picture I posted. I cannot created the folder you are asking for. [This](http://stackoverflow.com/questions/18232372/slide-a-layout-up-from-bottom-of-screen) is where I got my code. – the_prole Oct 01 '15 at 22:10
  • 1.Try with file manager of system just rename animator folder to anim. 2. Check with MainActivity.this – sswierczek Oct 01 '15 at 22:14
  • I don't think the name of the folder has anything to do with the problem to be honest. Congratulations, you solved the first error with `MainActivity.this`. Why did `this` not work? Now what of the second error? – the_prole Oct 01 '15 at 22:21
  • Oh I see, I was using the `context` inside of an on click listener, and did not implicitly refer to `context` of main function. Sorry, I should have posted the entire code so you could see the big picture. – the_prole Oct 01 '15 at 22:23
  • First doesn't work because You tried it in anonymous class of OnItemClickListener so as I said this is referring to this instance not to Activity. Have You got still some errors? – sswierczek Oct 01 '15 at 22:28
  • Thanks. Many apologies. Yes, I still have the second error. `Expected resource of type anim` I will try to add the `anim` folder with the file manager. Maybe Android Studio has a glitch? – the_prole Oct 01 '15 at 22:30
  • Yes try this, and after that restart Android Studio to be 100% sure. – sswierczek Oct 01 '15 at 22:32
0

First of all do you use this inside an activity or not?

I only know how to handle the first warning, sorry for that but I'm an amateur myself. If you are inside an activity you can get the context using:

this

or

getApplicationContext()

If you are outside your activity you will have to pass down your context to the class using it:

public class MainActivity extends Activity(){

     ThisClassNeedsContext test;

     public MainActivity(Context context){
          // other code
          test = new ThisClassNeedsContext(context);
     }
}

public class ThisClassNeedsContext { 
     public ThisClassNeedsContext(Context context){
          // now you have acces to the context
     }
}

I hope this helps! Good luck EDIT: Typo in explanation

  • I use it inside of an onclick listener in the main fucntion. See the code summary at the bottom of the post. – the_prole Oct 01 '15 at 22:18
  • Errr okay my apologies I made some mistakes here and there. I actually misinterpreted my own code '-_- as mentioned at the answer from Sebastian this should actually do the trick. I'll edit my answer. – Mattijn Wagt Oct 01 '15 at 22:21
  • Okay so I found another things that might work. See my code for the updated version. – Mattijn Wagt Oct 01 '15 at 22:30