gpt4 book ai didi

android - 获取 NullPointerException : Attempt to read from field 'int android.view.View.mViewFlags' - Android animation

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:44:58 24 4
gpt4 key购买 nike

我已经实现了一个动画,其中我在回收站 View 中有 ImageView ,在单击项目时我想在回收站 View 外的 ImageView 上做一些动画,这样- 我创建了一个新的 ImageView ,- 将其添加到主容器- 做了动画- 最后我将它从主容器中移除

这是回收站 View 的点击项目的代码:

代码:

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 个 View 上运行此动画时,它工作正常。但是,当我在同一个 View 上多次运行动画时,在运行另一个动画之前,我会收到以下错误。

错误日志:

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)

我试图在从主容器中移除 View 时放置 try-catch block ,但它并没有改变结果并且仍然出现相同的错误。

我也试过这个答案https://stackoverflow.com/a/4295570/2296787并没有解决问题

奇怪的是错误并没有告诉我异常发生的确切位置。

预先感谢您的帮助。

最佳答案

正如 @sasikumar 提到的解决方案是在真实设备 上运行测试

但我也在模拟器上解决了这个问题,方法是在动画结束监听器中删除 View 之前添加 postDelayed 并延迟 500 毫秒:

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) {
}
});

这适用于模拟器和真实设备

关于android - 获取 NullPointerException : Attempt to read from field 'int android.view.View.mViewFlags' - Android animation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41821727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com