0

this code correctly work in MainActivity, but i am want to put some animation methods in different class, and call it from there, but i have got an error i am trying to understand, why it shows me an error any ideas?

public class AnimationClass extends MainActivity{
    private static ImageButton heart_icon,bee_icon;
    private static int kill_flag = 0;
    private static Animation anim_heart, anim_bee;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        heart_icon=(ImageButton)findViewById(R.id.heart_icon);
        bee_icon = (ImageButton)findViewById(R.id.bee_icon);
    }

    public static void loadAnimations(Activity activity){
        anim_heart = AnimationUtils.loadAnimation(activity, R.anim.heart_anim);
        heart_icon.startAnimation(anim_heart);

        anim_bee = AnimationUtils.loadAnimation(activity, R.anim.bee_anim);
        bee_icon.startAnimation(anim_bee);
    }
}

Animation:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:shareInterpolator="true">
    <scale
        android:fillAfter="true"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:duration="1000"
        android:fromXScale="1.0"
        android:toXScale="1.2"
        android:fromYScale="1.0"
        android:toYScale="1.2"
        android:pivotX="50%"
        android:pivotY="50%"/>
    <rotate
        android:fillAfter="true"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:duration="1200"
        android:fromDegrees="-20"
        android:toDegrees="15"
        android:pivotX="80%"
        android:pivotY="20%"/>
    <translate
        android:fillAfter="true"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:duration="800"
        android:toXDelta="5"
        android:toYDelta="-5"/>
    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"
        android:duration="1500"/>

</set>

the error in:anim_heart = AnimationUtils.loadAnimation(activity, R.anim.heart_anim);

3
  • 1
    Post the full error message. Commented Feb 12, 2015 at 21:36
  • 1
    Before you try to find view by id you have to set your layout : setContentView(R.layout.your_layout); Commented Feb 12, 2015 at 21:43
  • @Josef thanks, but no, the problem was not here. it looks like the correct answer was given by user3249477 Commented Feb 13, 2015 at 6:02

1 Answer 1

1

You seem to misunderstand how static variables and methods work.

When you create an object of AnimationClass:

new AnimationClass()

onCreate is called and the two ImageButtons are set properly.

However when you call the static method loadAnimations, the ImageButton variables might be still null, which in return will throw the NPE.

Different solutions:

  1. Check for null variables:

    public static void loadAnimations(Activity activity){
        anim_heart = AnimationUtils.loadAnimation(activity, R.anim.heart_anim);
        if (heart_icon != null) {
            heart_icon.startAnimation(anim_heart);
        }
    
        anim_bee = AnimationUtils.loadAnimation(activity, R.anim.bee_anim);
        if (bee_icon != null) {
            bee_icon.startAnimation(anim_bee);
        }
    }
    
  2. If the Activity you pass as an argument to loadAnimations contains the views in question, you may want to find them on the spot:

    public static void loadAnimations(Activity activity){
        heart_icon = (ImageButton) activity.findViewById(R.id.heart_icon);
        bee_icon = (ImageButton) activity.findViewById(R.id.bee_icon);
    
        anim_heart = AnimationUtils.loadAnimation(activity, R.anim.heart_anim);
        heart_icon.startAnimation(anim_heart);
    
        anim_bee = AnimationUtils.loadAnimation(activity, R.anim.bee_anim);
        bee_icon.startAnimation(anim_bee);
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

all of this two var work, but: 1. in this case, animation does not start, becouse heart_icon is not null , if i understarnd it correctly... 2. in that case animation work good, but if i need to use heart_icon in this class but in different static void, i will be needet to repet this code again? how to fix it and make easier?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.