使用三元运算符时类型anim的预期资源

时间:2017-05-09 13:09:11

标签: android android-lint

这是一个非常愚蠢的编译器错误,我想知道是否有一种简单的方法来抑制它(例如使用注释)?

错误发生在setCustomAnimations()的第二个参数上。错误是: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

如果我将三元线扩展为以下任一项,则错误消失。

int exit_animation;

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

或者:

int exit_animation = 0;

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

1 个答案:

答案 0 :(得分:1)

抑制错误的解决方案是:

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

在评论中归功于CommonsWare。