获取NullPointerException:尝试从字段“ int android.view.View.mViewFlags”读取-Android动画 [英] getting NullPointerException: Attempt to read from field 'int android.view.View.mViewFlags' - Android animation

查看:87
本文介绍了获取NullPointerException:尝试从字段“ int android.view.View.mViewFlags”读取-Android动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个动画,在回收者视图中有imageview,单击该项目后我想在回收者视图之外的图像视图上做一些动画,这样
-我创建了一个新的imageview
-将其添加到主容器中
-对其进行动画处理
-最后我将其从主容器中删除

I have implemented an animation where i have imageview in recycler view, on clicking the item i want to do some animation on the image view outside the recycler view, so that - I created a new imageview, - added it to the main container - did the animation on it - lastly I remove it from the main container

这里是回收者视图中单击项的代码:

Here is the code for the on click item of the recycler 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个视图上运行此动画时,它可以正常工作。但是,当我在同一视图上多次运行动画时,以及在一个视图在我运行另一个视图之前未完成时,都会出现以下错误。

When I run this animation on 1 view, It works correctly. However, I get the error below when I run the animation many times on same view, and before one finishes when I run the another.

错误日志:

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块,但没有更改结果,仍然出现相同的错误。

I tried to put try-catch block when removing the view from the main container, but it did not change the result and still getting the same error.

我也尝试了此答案 https://stackoverflow.com/a/4295570/2296787 并没有解决问题

I tried also this answer https://stackoverflow.com/a/4295570/2296787 and it did not solve the problem

奇怪的是该错误并没有显示出我在哪里

The strange is that the error doesnt show me where exactly the exception raises.

在此先感谢您的帮助。

推荐答案

@sasikumar 所述,解决方案是在真实数据中心上运行测试

As @sasikumar has mentioned the solution was to run the test on real device

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

but also i solved it on the emulator also by adding postDelayed with 500 milliseconds delay before removing the view in the animation end listener:

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

这对模拟器和真实设备都有效

This works for both Emulator and Real Devices

这篇关于获取NullPointerException:尝试从字段“ int android.view.View.mViewFlags”读取-Android动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆