getting NullPointerException: Attempt to read from

2019-06-24 00:28发布

问题:

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:

Code:

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

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.

Error Log:

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)

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.

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.

Thank you in advance for helping.

回答1:

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

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



回答2:

In emulator it is not working. Run on real device then It is working fine..