Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected resource of type animator [ResourceType]

I've updated my SDK to the newest version, but now I am getting a lint error.

Error: Expected resource of type animator [ResourceType]

The error occurs on this line:

AnimatorInflater.loadAnimator(context, R.anim.right_slide_in) 

The referenced resource /res/anim/right_slide_in.xml looks like this:

<?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"     android:interpolator="@android:anim/linear_interpolator"     android:valueTo="0"     android:valueFrom="1.0"     android:propertyName="xFraction"     android:valueType="floatType"     android:duration="450" /> 

It always worked before. Could somebody explain why I am getting that error?

like image 962
Marcin Kunert Avatar asked Dec 15 '15 15:12

Marcin Kunert


1 Answers

The error occurs because you store your Animator resources in the wrong directory! It worked before because the AnimatorInflater can load the xml regardless of in which folder it is saved.

  • R.anim.* resources from the res/anim/ folder are for view animations.
  • R.animator.* resources from the /res/animator/ folder are for Animators.

So to fix the error just move your Animator resources from /res/anim/ to /res/animator/.


This used to make no difference at all until the resource type annotations were added to the support library. A long with those annotations there also were many new lint checks among others the one which tripped you up.

In the future if you get an error like this you can look at the annotation to figure out what you are doing wrong. For example the implementation of loadAnimator() of the AnimatorInflater looks like this:

public static Animator loadAnimator(Context context, @AnimatorRes int id)         throws NotFoundException {     return loadAnimator(context.getResources(), context.getTheme(), id); } 

The @AnimatorRes annotation on the id parameter indicates that only Animator resources should be passed here. If you look at the documentation of @AnimatorRes it reads like this:

Denotes that an integer parameter, field or method return value is expected to be an animator resource reference (e.g. android.R.animator.fade_in).

If the description doesn't explain the error already, then the example usually does ;)

like image 163
Xaver Kapeller Avatar answered Sep 23 '22 09:09

Xaver Kapeller