0

我有一个测试活动,它应该完成非常简单的任务:膨胀视图,并使用 ObjectAnimator 在屏幕上移动它。

如果我使用 NineOldAndroids 库,这适用于所有设备(OS 2.2+ - OS4.2)。

但是,如果我将库更改为 ActionBarSherlock(假设内部有相同的 NineOldAndroid 库),它适用于 OS 3.x 及更高版本,但不适用于 2.2 和 2.3 设备。在这种情况下,它找不到属性并抛出空指针异常(如下所列)。

有人有同样的问题吗?是否有解决方法,或者我实际上已经使用了两个库(NineOldAndroids 和 ActionBarSherlock )来解决问题?

这是TestsActivity

public class TestActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    RelativeLayout mainLayout =  (RelativeLayout) findViewById(R.id.main_layout);

    View testView = getLayoutInflater().inflate(R.layout.object_holder, null);

    mainLayout.addView(testView);

    AnimatorSet set = new AnimatorSet();
    set.playTogether(
        ObjectAnimator.ofFloat(testView, "translationX", 100),
        ObjectAnimator.ofFloat(testView, "translationY", 100)
    );
    set.setDuration(0).start();

}
}

(同样的事情发生,如果我用 "x" 改变 "translationX" 。)

这是例外(仅当我在 2.2 和 2.3 设备上使用库 ActionBarSherlock 时才会发生这种情况):

05-16 13:24:53.662: E/PropertyValuesHolder(2007): Couldn't find setter property translationX for RelativeLayout with value type float
05-16 13:24:53.662: E/PropertyValuesHolder(2007): RelativeLayout - Couldn't find no-arg method for property translationX: java.lang.NoSuchMethodException: getTranslationX
05-16 13:24:53.662: D/AndroidRuntime(2007): Shutting down VM
05-16 13:24:53.662: W/dalvikvm(2007): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
05-16 13:24:53.672: E/AndroidRuntime(2007): FATAL EXCEPTION: main
05-16 13:24:53.672: E/AndroidRuntime(2007): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testanimator/com.example.testanimator.TestActivity}: java.lang.NullPointerException
05-16 13:24:53.672: E/AndroidRuntime(2007):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1821)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at android.os.Looper.loop(Looper.java:150)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at android.app.ActivityThread.main(ActivityThread.java:4263)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at java.lang.reflect.Method.invokeNative(Native Method)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at java.lang.reflect.Method.invoke(Method.java:507)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at dalvik.system.NativeStart.main(Native Method)
05-16 13:24:53.672: E/AndroidRuntime(2007): Caused by: java.lang.NullPointerException
05-16 13:24:53.672: E/AndroidRuntime(2007):     at com.actionbarsherlock.internal.nineoldandroids.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:515)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at com.actionbarsherlock.internal.nineoldandroids.animation.ObjectAnimator.initAnimation(ObjectAnimator.java:386)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at com.actionbarsherlock.internal.nineoldandroids.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:539)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at com.actionbarsherlock.internal.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:929)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at com.actionbarsherlock.internal.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:952)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at com.actionbarsherlock.internal.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:364)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at com.actionbarsherlock.internal.nineoldandroids.animation.AnimatorSet.start(AnimatorSet.java:501)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at com.example.testanimator.TestActivity.onCreate(TestActivity.java:30)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
05-16 13:24:53.672: E/AndroidRuntime(2007):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
05-16 13:24:53.672: E/AndroidRuntime(2007):     ... 11 more
4

1 回答 1

0

您需要将 Nineoldandroids 添加为项目库或 jar 以及 actionbarsherlock。

目前,您的代码使用的是 Nineoldandroids 的内部 actionbarsherlock 版本,它并不真正适合客户端开发人员使用。您可以在同一个项目中同时使用 Nineoldandroids 和 actionbarsherlock。

我建议您使用 ViewHelper.setTranslationX() 和 ViewHelper.setTranslationY() 方法来完成您想做的事情。

于 2013-07-02T17:00:39.227 回答