Search code examples
androidandroid-actionbaractionbarsherlockandroid-animationandroid-fragmentactivity

NullPointerException with setAnimationListener() in FragmentActivity


I'm trying to get a viewflipper to flip. Seems to work in a regular activity, but when I went to change everything into fragments I keep getting NPE's.

Here's my code:

Animation in, out;
ViewFlipper customActionBarFlipper;

MyFragmentActivity mfa = MyFragmentActivity.this;
Context ctx = mfa; //Originally I was using this context in the loadAnimation.

in = AnimationUtils.loadAnimation(getBaseContext(), R.anim.push_left_in_80);
out = AnimationUtils.loadAnimation(getBaseContext(), R.anim.push_left_out_80);

in.setAnimationListener(mfa);
out.setAnimationListener(mfa);

customActionBarFlipper.setInAnimation(in);
customActionBarFlipper.setOutAnimation(out);

I have a really good feeling that I'm just forgetting the obvious, but lack of sleep is killer. If anyone could be so kind and push me in the right direction, that would be appreciated.

If you would like to see more code I'll gladly provide it. I think this should be enough. The layout that has the flipper is inflated too.

Thanks!


Solution

  • Your customActionBarFlipper isn't initialized anywhere. You just create it, and then try to use it without initializing it. This makes it null, and results in a NullPointerException.

    Your flipper may be available in the inflated layout, but you must still get a reference to it.