1

我正在使用NineOldAndroid库来执行动画。API<=10 的动画效果很好。但是对于 API>10,应用程序强制关闭。这是我的代码:

import static com.nineoldandroids.view.ViewPropertyAnimator.animate;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.MenuItem;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.ObjectAnimator;

public class ActivityActualMain extends SherlockActivity  {
    LinearLayout container1, container2;
    RelativeLayout viewTree;
    ImageView image, image1, image2;
    TextView tv, tv1, tv2, tv3, tv4;
    ObjectAnimator anim;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final int duration = 2000;
    setContentView(R.layout.activity_actual_main);

    ActionBar bar = getSupportActionBar();
    bar.setDisplayHomeAsUpEnabled(true);
    bar.setBackgroundDrawable(getResources().getDrawable(
            R.drawable.red_actionbar));
    viewTree = (RelativeLayout) findViewById(R.id.viewTree);
    container1 = (LinearLayout) findViewById(R.id.linearLayout1);
    container2 = (LinearLayout) findViewById(R.id.linearLayout2);
    image = (ImageView) findViewById(R.id.imageView1);
    image1 = (ImageView) findViewById(R.id.imageView2);
    image2 = (ImageView) findViewById(R.id.imageView3);
    tv = (TextView) findViewById(R.id.text_tech_des);

    viewTree.getViewTreeObserver().addOnGlobalLayoutListener(//to check if the layout has been placed in activity
            new OnGlobalLayoutListener() {
                public void onGlobalLayout() {
                    if (Build.VERSION.SDK_INT < 16) {
                        viewTree.getViewTreeObserver().removeGlobalOnLayoutListener(this);}
                    else{
                        viewTree.getViewTreeObserver()
                        .removeOnGlobalLayoutListener(this);
                    }

                    anim = ObjectAnimator.ofFloat(image, "y", 0f,
                            image.getTop());

                    anim.addListener(new AnimatorListener() {

                        @Override
                        public void onAnimationStart(Animator arg0) {
                            // TODO Auto-generated method stub
                            ObjectAnimator.ofFloat(tv, "alpha", 1, 0, 1)
                                    .setDuration(duration).start();//line no 82
                            ObjectAnimator.ofFloat(container1, "x", 0f,
                                    container1.getLeft()).setDuration(1000).start();
                            ObjectAnimator.ofFloat(container2, "x", 0f,
                                    container2.getLeft()).setDuration(1000).start();
                        }

                        @Override
                        public void onAnimationRepeat(Animator arg0) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onAnimationEnd(Animator arg0) {
                            // TODO Auto-generated method stub
                        }

                        @Override
                        public void onAnimationCancel(Animator arg0) {
                            // TODO Auto-generated method stub

                        }
                    });
                    anim.setDuration(duration).start();//line no 106
                }
            });

}
}

这是我的堆栈跟踪:

    10-26 19:23:15.203: E/AndroidRuntime(21541): FATAL EXCEPTION: main
10-26 19:23:15.203: E/AndroidRuntime(21541): java.lang.NullPointerException
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.nineoldandroids.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:523)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.nineoldandroids.animation.ObjectAnimator.initAnimation(ObjectAnimator.java:410)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.nineoldandroids.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:538)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:928)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:385)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.vishalaksh.technex.ActivityActualMain$2$1.onAnimationStart(ActivityActualMain.java:82)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:937)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:385)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.vishalaksh.technex.ActivityActualMain$2.onGlobalLayout(ActivityActualMain.java:106)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:808)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1768)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.view.Choreographer.doFrame(Choreographer.java:532)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.os.Handler.handleCallback(Handler.java:730)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.os.Looper.loop(Looper.java:137)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.app.ActivityThread.main(ActivityThread.java:5103)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at java.lang.reflect.Method.invokeNative(Native Method)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at java.lang.reflect.Method.invoke(Method.java:525)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

我在设备 API < 10 上遇到了这个库的问题,它给出了相同的堆栈跟踪。鉴于我使用的是 NineOldAndroids 的源代码,我决定调查库中的问题。

经过一些测试后,我注意到发生此错误是因为 lib 尝试调用视图上不存在的一些方法(因为它是旧的 API 级别)。再搜索一下,我发现了 AnimatorProxy 类,它有一个名为“wrap”的静态方法。此方法用于封装旧 Android 版本中的 View 对象,模拟某些动画方法的存在,例如 setScaleX/Y、setTransalationX/Y。

为了解决这个问题,我必须打开 ObjectAnimator 类并搜索所有出现的这一行(在我的库代码中,我发现了 4 次出现):

mTarget = target;

在该类中,我创建了以下方法:

private void setTarget(Object obj) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB && obj instanceof View) {
        mTarget = AnimatorProxy.wrap((View) obj);
    } else {
        mTarget = obj;
    }
}

并将上面的行替换为:

setTarget(target);

不知道它是否可以解决您的问题,因为您说它发生在 API 10+(与我的相反)上,但它是一个很好的起点。

于 2013-10-31T17:18:01.520 回答