Endi Tóth
Endi Tóth

Reputation: 221

Start animation from another class

I'm trying to do a simple fade in, fade out animation in my activity by calling it from another class. Actually I have one Activity and two simple class. In my Activiy I call a method from the first class like this:

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

    txt = (TextView)findViewById(R.id.hello);

    Megegy me = new Megegy(txt,MainActivity.this);
    me.animatika();
}

Here is this Megegy class:

public class Megegy {
TextView txt;
Activity activity;

public Megegy(TextView txt,Activity activity){
    this.activity = activity;
    this.txt = txt;
}

Fadede fa = new Fadede(activity,txt);

public void animatika(){
    fa.animati();
}
}

In this class I call a method from another class which contain's the animation:

public class Fadede implements Animation.AnimationListener {

Animation animFadein;
Animation animFadeout;
public TextView txt;
Activity activity;

public Fadede(Activity activity, TextView txt) {
    this.activity = activity;
    this.txt = txt;
}

public void animati(){
    animFadein = AnimationUtils.loadAnimation(activity, R.anim.fadein);
    animFadein.setAnimationListener(this);
    animFadeout = AnimationUtils.loadAnimation(activity, R.anim.fadeout);
    animFadeout.setAnimationListener(this);

    txt.startAnimation(animFadein);
}

public void onAnimationEnd(Animation animation) {
    // Take any action after completing the animation

    // check for fade in animation
    if (animation == animFadein) {
    }
}

public void onAnimationRepeat(Animation animation) {
    // TODO Auto-generated method stub

}

public void onAnimationStart(Animation animation) {
    // TODO Auto-generated method stub

}
}

And the problem is when I run this app I got a java.lang.NullPointerException for this line:

          animFadein = AnimationUtils.loadAnimation(activity, R.anim.fadein);

I have no idea what causes this error, but if anyone has one please response!!

Upvotes: 1

Views: 484

Answers (2)

Ichor de Dionysos
Ichor de Dionysos

Reputation: 1137

Notice that the System first creates all off your global variables in your class and give them the value you specified or the value of null (if not), this would look like this:

TextView txt = null;
Activity activity = null;
Fadede fa = new Fadede(activity,txt); //activity and txt are both null

And after your maschine did this he calls the constructor, so you have to call

new Fadede(activity, txt);

in your constructor after you gave activity and txt a value

Upvotes: 1

RogueBaneling
RogueBaneling

Reputation: 4471

Instead of passing a Activity object, try just passing a Context. And in your activity, use Megegy me = new Megegy(txt, this);

Also,

public class Megegy {
    TextView txt;
    Activity activity;
    Fadede fa;

    public Megegy(TextView txt, Context context){
        this.activity = activity;
        this.txt = txt;
        this.fa = new Fadede(context, txt);
    }
}

Right now the Fadede object is being constructed while activity and txt are still null, so instead you should create the object from within Megegy's constructor.

Upvotes: 2

Related Questions