从Framelayout删除动画视图的Nullpointer

时间:2015-09-01 17:13:50

标签: android animation nullpointerexception imageview

我在扩展AnimationSet的类中有一个ImageView。 ImageView是动画的,之后应该删除。

这是发生这些事情的类:

public class TrackerAnimation extends AnimationSet {

private ImageView imageView;
private FrameLayout context_layout;

/** Constructor
* context = Activity
* context_layout = Framelayout in which the ImageView will be added programmatically
*
*/
public TrackerAnimation(Context context, FrameLayout context_layout, ...){
    //...
    this.imageView = new ImageView(context);
    this.context_layout.addView(imageView);

    this.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        TrackerAnimation.this.onAnimationStart();
        }

        @Override
        public void onAnimationEnd(Animation animation) {
        TrackerAnimation.this.onAnimationEnd();
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
}

@Override
public void start() {
    imageView.startAnimation(animationSet);
}

// Invoked by the AnimationListener, created at Constructor
public void onAnimationStart() {
    //empty
}

// Invoked by the AnimationListener, created at Constructor
public void onAnimationEnd() {
    // ! context_layout and imageView are both NOT NULL at this point
    this.context_layout.removeView(imageView);
}
}

如果用户按下Activity中的按钮,则会创建并启动TrackerAnimation的实例。动画完成后,AnimationListener调用TrackerAnimation的onAnimationEnd()(是的..它与AnimationListener的接口方法同名)。

如果您连续两次按下按钮并且第一个动画仍在运行,则应用程序崩溃并显示以下消息:

09-01 18:42:08.678: E/AndroidRuntime(5046): FATAL EXCEPTION: main
09-01 18:42:08.678: E/AndroidRuntime(5046): Process: com.abc.def, PID: 5046
09-01 18:42:08.678: E/AndroidRuntime(5046): java.lang.NullPointerException: Attempt to read from field 'int android.view.View.mViewFlags' on a null object reference
09-01 18:42:08.678: E/AndroidRuntime(5046):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3197)
09-01 18:42:08.678: E/AndroidRuntime(5046):     at android.view.View.updateDisplayListIfDirty(View.java:14162)
09-01 18:42:08.678: E/AndroidRuntime(5046):     at android.view.View.getDisplayList(View.java:14189)
09-01 18:42:08.678: E/AndroidRuntime(5046):     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
09-01 18:42:08.678: E/AndroidRuntime(5046):     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
09-01 18:42:08.678: E/AndroidRuntime(5046):     at android.view.View.updateDisplayListIfDirty(View.java:14127)
09-01 18:42:08.678: E/AndroidRuntime(5046):     at android.view.View.getDisplayList(View.java:14189)
09-01 18:42:08.678: E/AndroidRuntime(5046):     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
09-01 18:42:08.678: E/AndroidRuntime(5046):     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
09-01 18:42:08.678: E/AndroidRuntime(5046):     at android.view.View.updateDisplayListIfDirty(View.java:14127)
09-01 18:42:08.678: E/AndroidRuntime(5046):     at android.view.View.getDisplayList(View.java:14189)
09-01 18:42:08.678: E/AndroidRuntime(5046):     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
09-01 18:42:08.678: E/AndroidRuntime(5046):     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
09-01 18:42:08.678: E/AndroidRuntime(5046):     at android.view.View.updateDisplayListIfDirty(View.java:14127)
09-01 18:42:08.678: E/AndroidRuntime(5046):     at android.view.View.getDisplayList(View.java:14189)

由于没有进一步暗示虚拟机内部出现什么问题,我不知道如何搜索此问题的解决方案。我甚至无法调试,因为在调试时模拟doubleClick似乎很难。

在layout.xml中有一个嵌套的Framelayout:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/map_fragment"
        class="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center" />

    <Button
        android:id="@+id/btn_tracker"
        android:layout_width="120sp"
        android:layout_height="120sp"
        android:layout_gravity="bottom|center_horizontal"
        android:background="@drawable/btn_tracker" />

    <Button
        android:id="@+id/btn_my_location"
        android:layout_width="90sp"
        android:layout_height="90sp"
        android:layout_gravity="top|right"
        android:background="@drawable/btn_my_location" />

    <FrameLayout
        android:id="@+id/fl_map_overlay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">


    </FrameLayout>

</FrameLayout>

具有id“fl_map_overlay”的内部FrameLayout是ViewImages转到的内容。并且它们也应该通过上面显示的回调内容从中删除。

早期的尝试是将ImageView作为WeakReference进行管理......但我想这并不能真正解决Nullpointer的问题。

我感谢任何帮助!

编辑:XML中的硬编码变量很糟糕......我知道; D

0 个答案:

没有答案