AOS 4.x error with animation

I have a popup window class that works fine in aos 2.x (like 2.2.2, 2.3.5, etc.), but crashes in aos 4.x. The error code is as follows:

public void dismissPopup(){
    if (!isVisible)
        return;
    isVisible = false;

    final Animation animation = AnimationUtils.loadAnimation(activity, R.anim.popup_hide);
    animation.setAnimationListener(new AnimationListener() {
        public void onAnimationEnd(final Animation animation) {
            // The animation has ended
            popupWindow.dismiss();
        }
        public void onAnimationRepeat(final Animation animation) {}
        public void onAnimationStart(final Animation animation) {}
    });
    popupView.startAnimation(animation);
}

to make it work in aos 4.x I need to comment out all the lines of the animation, like 2b:

public void dismissPopup(){
    if (!isVisible)
        return;
    isVisible = false;

    popupWindow.dismiss();
}

this works fine in aos 4.1.x but does not provide animations. What could be here? Doesn't android support backward compatibility? crash log

04-25 21:05:50.387: E/AndroidRuntime(8997): FATAL EXCEPTION: main
04-25 21:05:50.387: E/AndroidRuntime(8997): java.lang.NullPointerException
04-25 21:05:50.387: E/AndroidRuntime(8997):     at android.view.ViewRootImpl.drawAccessibilityFocusedDrawableIfNeeded(ViewRootImpl.java:2301)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at android.view.ViewRootImpl.onHardwarePostDraw(ViewRootImpl.java:1931)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1182)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at android.view.ViewRootImpl.draw(ViewRootImpl.java:2147)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2019)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1830)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:736)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at android.view.Choreographer.doCallbacks(Choreographer.java:566)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at android.view.Choreographer.doFrame(Choreographer.java:536)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:722)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at android.os.Handler.handleCallback(Handler.java:615)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at android.os.Looper.loop(Looper.java:137)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at android.app.ActivityThread.main(ActivityThread.java:4745)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at java.lang.reflect.Method.invokeNative(Native Method)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at java.lang.reflect.Method.invoke(Method.java:511)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-25 21:05:50.387: E/AndroidRuntime(8997):     at dalvik.system.NativeStart.main(Native Method)

UPD animation: works in version 4.0.3, but crashes in 4.1.1

+1
source share
1 answer

Its stupid, but I found a workaround:

animation.setAnimationListener(new AnimationListener() {
    public void onAnimationEnd(final Animation animation) {
        // The animation has ended
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                popupWindow.dismiss();
            }
        });
    }
    public void onAnimationRepeat(final Animation animation) {}
    public void onAnimationStart(final Animation animation) {}
});

I can’t even imagine why this helps and why it does not work without it in 4.1.1 ...

+2
source

Source: https://habr.com/ru/post/1795884/


All Articles