添加监听器导致崩溃?

时间:2012-09-21 19:26:02

标签: java android

我有一个监听器界面:

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

此类保留一个列表:

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);
    }
...

该类实现接口:

公共类PuzzleView扩展了SurfaceView实现的Runnable,AnimationListener { ...     private AnimationManager animationManager = null;

...

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

当我添加动画侦听器时,它会崩溃。可能有什么不对?

由于

  

09-21 19:15:51.433:D / AndroidRuntime(278):关闭VM 09-21   19:15:51.433:W / dalvikvm(278):threadid = 1:线程退出   未捕获的异常(组= 0x4001d800)09-21 19:15:51.453:   E / AndroidRuntime(278):致命例外:主09-21 19:15:51.453:   E / AndroidRuntime(278):java.lang.RuntimeException:无法启动   活动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):引起: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更多09-21   19:15:53.333:I / Process(278):发送信号。 PID:278 SIG:9

2 个答案:

答案 0 :(得分:2)

看起来你永远不会初始化AnimationManager类中的任何列表。您应该为AnimationManager构造函数中的每个列表添加类似的内容:

animationListeners = new ArrayList<AnimationListener>();

答案 1 :(得分:0)

尝试使用:

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() {

    }); 
}