【问题标题】:Couldn't find setter property rotation for Button with value type float找不到值类型为 float 的 Button 的 setter 属性旋转
【发布时间】:2013-11-02 20:45:20
【问题描述】:

当用户点击按钮时,我正在使用 NineOldAndroids 来旋转按钮。以下是代码:

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

当我点击按钮时,它会将这个日志扔到 logcat 中:

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

我进行了多次搜索,但在这里没有找到任何解决方案。 请帮帮我。

谢谢。

【问题讨论】:

  • 查看我的更新答案

标签: android android-animation nineoldandroids


【解决方案1】:

也许视图还没有准备好?

这些代码在我的设备(4.3,Nexus4)上运行良好。

【讨论】:

    【解决方案2】:

    您可以按照PropertyValuesHolder: Couldn't find setter/getter for property alpha with value type float 此处的答案尝试将所有 PropertyValuesHolder 替换为 ObjectAnimator(带有浮点变量)。

    或者你可能想试试下面的旋转动画:

    在 res/drawable/anim 中创建一个动画 XML

    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>
    

    创建一个旋转动画。

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

    然后,当用户单击按钮时,在按钮中使用 rotate() 动画。

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

    【讨论】:

      【解决方案3】:

      编辑:ObjectAnimator 从 API 级别 11 开始可用。您的设备是 API 级别 9 。尝试在至少具有 Android 3.0+ 的设备上运行您的代码。

      【讨论】:

      • Couldn't find setter property translationX for Button with value type float。也不行。我正在根据 Htc 的欲望测试我的应用程序 - android 2.3。
      • ObjectAnimatorAPI 级别 11 起可用。您的设备是 API 级别 9 。尝试在至少有 Android 3.0+ 的设备上运行您的代码
      【解决方案4】:

      终于,我找到了根本原因。我同时使用NineOldAndroidsActionbarSherlock。然后,因为ActionbarSherlock已经包含了NineOldAndroids的一部分,所以我导入了com.actionbarsherlock.internal.nineoldandroids.animation.ObjectAnimator而不是com.nineoldandroids.animation.ObjectAnimator

      NineOldAndroids 适用于所有 android SDK 版本。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-07
        • 1970-01-01
        • 2017-12-16
        • 2016-01-10
        • 2018-06-20
        • 1970-01-01
        • 2013-10-27
        • 2018-09-24
        相关资源
        最近更新 更多