Search code examples
androidexceptionanimationmobilenosuchmethoderror

NoSuchMethodError on certain android phones


I am using a value animator to change status bar color with animation, works like a charm on my phone and emulator, but when I sent it to another person, it crashes with NoSuchMethodError, I couldn't figure out why,

  • and if there are methods that are available on certain phones, how would I check that?

  • And are there other ways to animate color change for status bar?

code below:

final ValueAnimator statusBarAnimator = ValueAnimator.ofArgb
            (ContextCompat.getColor(MainScreen.this, R.color.colorPrimary),
                    ContextCompat.getColor(MainScreen.this, R.color.colorPrimaryDark));
    statusBarAnimator.setDuration(500);
    statusBarAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        // how it works is that every time it updates, it goes to change the color by a little bit
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            if (Build.VERSION.SDK_INT >= 21) {
                getWindow().setStatusBarColor((Integer) statusBarAnimator.getAnimatedValue());
            }
        }
    });

Error code is here (from Crash Analytics):

Fatal Exception: java.lang.NoSuchMethodError: android.animation.ValueAnimator.ofArgb
   at com.peter.georeminder.MainScreen.initView(MainScreen.java)
   at com.peter.georeminder.MainScreen.onCreate(MainScreen.java)
   at android.app.Activity.performCreate(Activity.java)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
   at android.app.ActivityThread.access$1100(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
   at android.os.Handler.dispatchMessage(Handler.java)
   at android.os.Looper.loop(Looper.java)
   at android.app.ActivityThread.main(ActivityThread.java)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
   at dalvik.system.NativeStart.main(NativeStart.java)

Solution

  • ValueAnimator.ofArgb() was added in API21. To achieve this effect on lower API's, use ArgbEvaluator (added in API11).