У меня есть класс Animation, который я вызываю из основного класса, когда мне нужно переместить изображение из одного места в другое ... Но дело в том, что когда я его вызываю, это превращает меня в NullPointerException. Вот мой код:
public class Animation extends Activity{
ImageView image;
int width;
int fromXDelta;
AnimationSet animation = new AnimationSet(true);
TranslateAnimation translateAnimation = null;
Animation(View main) {
image = (ImageView) main.findViewById(R.id.logo);
fromXDelta = image.getLeft();
// this line gives me error:
width = getWindowManager().getDefaultDisplay().getWidth();
}
public void animateToLeft () {
translateAnimation = new TranslateAnimation(0, -fromXDelta, 0, 0);
translateAnimation.setDuration(1000);
animation.addAnimation(translateAnimation);
animation.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(android.view.animation.Animation v) {
// TODO Auto-generated method stub
image.clearAnimation();
v.reset();
image.layout(0, 0, image.getWidth(), image.getHeight());
}
public void onAnimationRepeat(android.view.animation.Animation arg0) { }
public void onAnimationStart(android.view.animation.Animation arg0) { }
});
image.startAnimation(animation);
}
public void animateToRight () {
translateAnimation = new TranslateAnimation(fromXDelta, width - image.getWidth() - fromXDelta , 0, 0);
translateAnimation.setDuration(1000);
animation.addAnimation(translateAnimation);
animation.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(android.view.animation.Animation v) {
// TODO Auto-generated method stub
image.clearAnimation();
v.reset();
image.layout(width - image.getWidth(), 0, width, image.getHeight());
}
public void onAnimationRepeat(android.view.animation.Animation arg0) { }
public void onAnimationStart(android.view.animation.Animation arg0) { }
});
image.startAnimation(animation);
}
public void animateToEx (int control) {
if(control == 0) {
// left to ex point
translateAnimation = new TranslateAnimation(fromXDelta, width - image.getWidth() - fromXDelta , 0, 0);
} else if(control == 2) {
// right to ex point
translateAnimation = new TranslateAnimation(fromXDelta, width - image.getWidth() - fromXDelta , 0, 0);
}
translateAnimation.setDuration(1000);
animation.addAnimation(translateAnimation);
animation.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(android.view.animation.Animation v) {
// TODO Auto-generated method stub
image.clearAnimation();
v.reset();
image.layout(width - image.getWidth(), 0, width, image.getHeight());
}
public void onAnimationRepeat(android.view.animation.Animation arg0) { }
public void onAnimationStart(android.view.animation.Animation arg0) { }
});
image.startAnimation(animation);
}
}
Заранее спасибо.