Couldn't find setter property rotation for Button with value type float

851 Views Asked by At

I am using NineOldAndroids to rotate a button when users click on it. here are the codes:

Button btntest = (Button) findViewById(R.id.testbutton);
        btntest.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator.ofFloat(v, "rotation", 0f, 360f).start();
            }
        });

When I clicked on the button, it thrown this log into logcat:

10-24 05:25:42.394: E/PropertyValuesHolder(2387): Couldn't find setter property rotation for Button with value type float

I have took many searching but I didn't find any solution here. Please help me.

Thanks.

4

There are 4 best solutions below

0
On

Finally, I found my root cause. I am using both NineOldAndroids and ActionbarSherlock. And then, because ActionbarSherlock already include a part of NineOldAndroids, so I imported com.actionbarsherlock.internal.nineoldandroids.animation.ObjectAnimator instead of com.nineoldandroids.animation.ObjectAnimator.

NineOldAndroids works fine on all android SDK versions.

2
On

EDIT: the ObjectAnimator is available since API level 11 . Your device is API level 9 . Try running your code on device with at least Android 3.0+.

0
On

Maybe the view doesn't ready?

Those code works fine on my device(4.3, Nexus4).

0
On

You can try replacing all PropertyValuesHolder with ObjectAnimator (with float vars) as per the answer here PropertyValuesHolder: Couldn't find setter/getter for property alpha with value type float.

Or You might want to try the rotate animation below:

Create an Animation XML in the res/drawable/anim

rotate_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >

<rotate
    android:duration="800"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:repeatMode="restart"
    android:toDegrees="360" />
</set>

Create a Rotate Animation.

private Animation rotate() {
    Animation animation = AnimationUtils.loadAnimation(NameOfActivity.this, R.anim.rotate_animation);
    return animation;
}

Then, use the rotate() animation in your button when the user clicks it.

Button btntest = (Button) findViewById(R.id.testbutton);
    btntest.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            v.startAnimation(rotate());
        }
    });