Reputation: 2790
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);
Upvotes: 0
Views: 204
Reputation: 44118
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:
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);
}
}
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);
}
Upvotes: 1