Android动画报java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Paint.getAlpha()' on a null object reference

使用View Animation实现图片的移动、缩放效果,在动画完成时将view移除,报如下错误:

03-18 07:00:06.854: E/AndroidRuntime(28405): java.lang.NullPointerException: Attempt to read from field 'int android.view.View.mViewFlags' on a null object reference

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3197)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.View.draw(View.java:15017)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.ViewGroup.drawChild(ViewGroup.java:3404)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.View.draw(View.java:15117)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.View.draw(View.java:15022)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.ViewGroup.drawChild(ViewGroup.java:3404)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.View.draw(View.java:15017)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.ViewGroup.drawChild(ViewGroup.java:3404)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.View.draw(View.java:15017)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.ViewGroup.drawChild(ViewGroup.java:3404)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.View.draw(View.java:15117)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.widget.FrameLayout.draw(FrameLayout.java:592)

03-18 07:00:06.854: E/AndroidRuntime(28405): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2595)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2606)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2519)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2337)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1968)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.Choreographer.doCallbacks(Choreographer.java:580)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.Choreographer.doFrame(Choreographer.java:550)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.os.Handler.handleCallback(Handler.java:739)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.os.Handler.dispatchMessage(Handler.java:95)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.os.Looper.loop(Looper.java:135)

03-18 07:00:06.854: E/AndroidRuntime(28405): at android.app.ActivityThread.main(ActivityThread.java:5221)

03-18 07:00:06.854: E/AndroidRuntime(28405): at java.lang.reflect.Method.invoke(Native Method)

03-18 07:00:06.854: E/AndroidRuntime(28405): at java.lang.reflect.Method.invoke(Method.java:372)

03-18 07:00:06.854: E/AndroidRuntime(28405): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)

03-18 07:00:06.854: E/AndroidRuntime(28405): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

查阅资料后通过如下方式解决:

new Handler().post(new Runnable() {

@Override

public void run() {

container.removeView(icon);

}

});

另有一种说法是在android.view.animation.Animation.AnimationListener的onAnimationStart, onAnimationEnd, onAnimationRepeat 中不要addView或removeView,因为这个三个动画的回调方法都是在View.draw()中执行的,而在draw函数中不要addView或removeView。还没有验证。

在此先做记录,找时间验证下。

推荐阅读更多精彩内容