jmasterx
jmasterx

Reputation: 54193

Adding listener causes crash?

I have a listener interface:

public interface AnimationListener 
{
    public void onAnimationStarted(Animation animation);
    public void onAnimationFinished(Animation animation);
    public void onAnimationCanceled(Animation animation);
}

This class keeps a list:

public class AnimationManager 
{
    private List<Animation> animations;
    private List<AnimationListener> animationListeners;
    private List<Animation> animationsToRemove;
    private float rate;

    public AnimationManager(float rate)
    {
        setRate(rate);
    }

    public void addAnimationListener(AnimationListener listener)
    {
        animationListeners.add(listener);
    }

    public void removeAnimationListener(AnimationListener listener)
    {
        animationListeners.remove(listener);
    }
...

This class implements the interface:

public class PuzzleView extends SurfaceView implements Runnable, AnimationListener { ... private AnimationManager animationManager = null;

...

public PuzzleView(Context context) 
{
            ...
    animationManager = new AnimationManager(FRAME_RATE);
    //animationManager.addAnimationListener(this); THIS CRASHES
}

When I add the animation listener, it crashes. What could be wrong?

Thanks

09-21 19:15:51.433: D/AndroidRuntime(278): Shutting down VM 09-21 19:15:51.433: W/dalvikvm(278): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 09-21 19:15:51.453: E/AndroidRuntime(278): FATAL EXCEPTION: main 09-21 19:15:51.453: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.joshl.drop7/com.joshl.drop7.Game}: java.lang.NullPointerException 09-21 19:15:51.453: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 09-21 19:15:51.453: E/AndroidRuntime(278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 09-21 19:15:51.453: E/AndroidRuntime(278): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 09-21 19:15:51.453: E/AndroidRuntime(278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 09-21 19:15:51.453: E/AndroidRuntime(278): at android.os.Handler.dispatchMessage(Handler.java:99) 09-21 19:15:51.453: E/AndroidRuntime(278): at android.os.Looper.loop(Looper.java:123) 09-21 19:15:51.453: E/AndroidRuntime(278): at android.app.ActivityThread.main(ActivityThread.java:4627) 09-21 19:15:51.453: E/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method) 09-21 19:15:51.453: E/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521) 09-21 19:15:51.453: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 09-21 19:15:51.453: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 09-21 19:15:51.453: E/AndroidRuntime(278): at dalvik.system.NativeStart.main(Native Method) 09-21 19:15:51.453: E/AndroidRuntime(278): Caused by: java.lang.NullPointerException 09-21 19:15:51.453: E/AndroidRuntime(278): at com.joshl.drop7.AnimationManager.addAnimationListener(AnimationManager.java:19) 09-21 19:15:51.453: E/AndroidRuntime(278): at com.joshl.drop7.PuzzleView.(PuzzleView.java:82) 09-21 19:15:51.453: E/AndroidRuntime(278): at com.joshl.drop7.Game.onCreate(Game.java:18) 09-21 19:15:51.453: E/AndroidRuntime(278): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 09-21 19:15:51.453: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 09-21 19:15:51.453: E/AndroidRuntime(278): ... 11 more 09-21 19:15:53.333: I/Process(278): Sending signal. PID: 278 SIG: 9

Upvotes: 0

Views: 182

Answers (2)

MattDavis
MattDavis

Reputation: 5178

Looks like you never initialize any of the Lists in your AnimationManager class. You should add something like this for each of your lists in your AnimationManager constructor:

animationListeners = new ArrayList<AnimationListener>();

Upvotes: 2

Raghav Sood
Raghav Sood

Reputation: 82583

Try using:

public class PuzzleView implements AnimationListener {
....
public PuzzleView(Context context) 
{
            ...
    animationManager = new AnimationManager(FRAME_RATE);
    animationManager.addAnimationListener(this); 
}
.....

OR

public PuzzleView(Context context) 
{
            ...
    animationManager = new AnimationManager(FRAME_RATE);
    animationManager.addAnimationListener(new AnimationListener() {

    }); 
}

Upvotes: 0

Related Questions