ViewAnimator's OnDraw throws NullPointerException if you remove child on onAnimationEnd

This is not a question, more like sharing with others a problem I encountered and how I resolved it. Basically, I was trying to create a ViewAnimator, who would create additional children in response to user clicks. To clean up after I have animated the next View in, I put

outAnimation.setAnimationListener(listener);

and in AnimationListener

public void onAnimationEnd(Animation animation) {
    viewAnimator.removeView(out);
}

Now, the problem with above approach is, immediately after onAnimationEnd, it throws a NullPointerException. Basically, it means, ViewAnimator is still using child view that is being animated out to draw. Since I have removed it, there is null there. I have done my research, and basically, it appears this is a known bug. See: Android Animation is not finished on onAnimationEnd To resolve this, I have modified layout.

<ViewAnimator
    android:id="@+id/animator"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

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

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

and onAnimationEnd I can safely call container.removeAllViews(). To animate a new view in, I select the hidden container and

container.addView(newView);
animator.setDisplayedChild(animator.indexOfChild(container));

I will be happy to see your comments and advice.

Answers


I've run into this problem and used the view's post method to wait until the animation is really done:

      public void onAnimationEnd(Animation animation) {
        //Wait until the container has finished rendering to remove the items.
        view.post(new Runnable() {
          @Override
          public void run() {
            //remove view here
          }
        });
      }

I solved it. I have inAnimation and outAnimation. SO:

@Override
public void onAnimationEnd(Animation animation) {

    if(animationsFinished < 2) animationsFinished++;
    else{

        this.setInAnimation(null);  // Skipping this will cause trouble
        this.setOutAnimation(null); // Skipping this will cause trouble

        flipper.post(new Runnable(){

            @Override
            public void run() {
                flipper.removeView(previous);
            }

        });

        animationsFinished = 0;

    }


}

Need Your Help

LCS dynamic programming using one dimensional array

c++ algorithm data-structures dynamic-programming

I am trying to do dynamic programming for finding length of LCS. I have used two dimensional array for that. But for a large string it gives runtime error due to memory overflow. Please tell me How

Iterating circular way

scala scala-2.8 loops circular-list

I need iterate through a List but circular way. I need too add new elements to the list and iterate over all elements (olds and news elements), How I do it? Is there any data structure for them?