Search code examples
androidandroid-lint

Expected resource of type anim when using ternary operator


This is a very silly compiler error, and I'm wondering if there's a simple way of suppressing it (such as with an annotation) ?

The error occurs on the 2nd argument of setCustomAnimations(). The error is: Expected resource of type anim.

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

int exit_animation = current_popup == null ? 0 : current_popup.getExitAnimation();

transaction.setCustomAnimations( fragment.getEnterAnimation(), exit_animation ); //ERROR

If I expand the ternary line to either of the following, the error disappears.

int exit_animation;

if ( current_popup == null )
    exit_animation = 0;
else
    exit_animation = current_popup.getExitAnimation();

Or:

int exit_animation = 0;

if ( current_popup != null )
    exit_animation = current_popup.getExitAnimation();

Solution

  • The solution to suppressing the error is:

    @AnimRes
    int exit_animation = current_popup == null ? 0 : current_popup.getExitAnimation();
    

    Credit to CommonsWare in the comments.