我实现了一个动画,在回收器视图中有imageview,单击该项目后我想在回收器视图之外的图像视图上做一些动画,因此-我创建了一个新的imageview-将其添加到主容器中-完成了动画-最后我将其从主容器中删除
以下是“回收者”视图中单击项的代码:
码:
final ImageView iv = new ImageView(getContext()); iv.setImageDrawable(fromView.getDrawable()); iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE); iv.setLayoutParams(fromView.getLayoutParams()); v_mainContainer.addView(iv); AnimationSet animatinSet = new AnimationSet(false); animatinSet.setFillAfter(false); animatinSet.setFillBefore(true); animatinSet.setDuration(1500); animatinSet.setInterpolator(new DecelerateInterpolator()); // ... rest of the animation animatinSet.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { v_mainContainer.removeView(iv); } @Override public void onAnimationRepeat(Animation animation) { } }); animatinSet.setAnimationListener(animationListner); animatinSet.setDuration(1500); iv.startAnimation(animatinSet);
当我在1个视图上运行此动画时,它可以正常工作。但是,当我在同一视图上多次运行动画时,以及在一个视图上运行另一个动画之前,出现以下错误。
错误日志:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.mbh.testApp, PID: 9628 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:3549) at android.view.View.draw(View.java:17071) at android.view.View.updateDisplayListIfDirty(View.java:16050) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728) at android.view.View.updateDisplayListIfDirty(View.java:16013) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728) at android.view.View.updateDisplayListIfDirty(View.java:16013) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728) at android.view.View.updateDisplayListIfDirty(View.java:16013) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728) at android.view.View.updateDisplayListIfDirty(View.java:16013) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728) at android.view.View.updateDisplayListIfDirty(View.java:16013) at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:656) at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:662) at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:770) at android.view.ViewRootImpl.draw(ViewRootImpl.java:2796) at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2604) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2211) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871) at android.view.Choreographer.doCallbacks(Choreographer.java:683) at android.view.Choreographer.doFrame(Choreographer.java:619) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
从主容器中删除视图时,我尝试放置try-catch块,但它没有改变结果,仍然出现相同的错误。
我也尝试了这个答案/sf/ask/17360801/,它没有解决问题
奇怪的是,该错误并未向我显示异常引发的确切位置。
预先感谢您的帮助。
正如@sasikumar提到的,解决方案是在真实设备上运行测试
但是我也通过在动画结束监听器中删除视图之前添加具有500毫秒延迟的postDelayed在模拟器上解决了它:
animatinSet.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { iv.setVisibility(View.GONE); v_mainContainer.postDelayed(new Runnable() { @Override public void run() { v_mainContainer.removeView(iv); } }, 500); } @Override public void onAnimationRepeat(Animation animation) { } });
这适用于模拟器和真实设备