Im adding and then animating a simple ImageView inside a RelativeLayout whenever a button is clicked. Heres my current code:
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView imageview = new ImageView(getActivity());
imageview.setImageDrawable(getActivity().getDrawable(R.drawable.pic));
imageview.setLayoutParams(layoutParams);
mRelativeLayout.addView(imageview);
Animation anim = AnimationUtils.loadAnimation(getActivity(), R.anim.animation);
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
mRelativeLayout.removeView(imageview);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
imageview.startAnimation(anim);
}
This works fine for the most part, but if I rapidly click the button, the app crashes with the following NPE:
java.lang.NullPointerException: Attempt to read from field 'int android.view.View.mViewFlags' on a null object reference at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3398) at android.view.View.draw(View.java:16187) at android.view.View.updateDisplayListIfDirty(View.java:15180) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573) at android.view.View.updateDisplayListIfDirty(View.java:15140) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573) at android.view.View.updateDisplayListIfDirty(View.java:15140) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
Any ideas as to why this is happening? Whats the fix?