[为FlipperView设置点时,动画时为NPE

问题描述 投票:0回答:1

我有一个非常简单的活动(One.class),其中展示了ViewFlipper + LinearLayout,其中我安装了一些点,这些点显示了视图移动的进度(实际上是一些RelativeLayout)。

public class One extends Activity {

private ViewFlipper viewFlipper;
private float lastX;
private LinearLayout mDotsLayout;
private int mDotsCount;
static ImageView Points[];

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.one);
    viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);

    mDotsLayout = (LinearLayout)findViewById(R.id.image_count);

    mDotsCount = viewFlipper.getChildCount();

    Points = new ImageView[mDotsCount];

    for (int i = 0; i < mDotsCount; i++) {
        Points[i] = new ImageView(this);
        Points[i].setImageResource(R.drawable.hide);
        mDotsLayout.addView(Points[i]);
    }

    One.Points[0].setImageResource(R.drawable.clip);


    viewFlipper.getAnimation().setAnimationListener(new Animation.AnimationListener() {

        public void onAnimationStart(Animation animation) {
        }

        public void onAnimationRepeat(Animation animation) {
        }

        public void onAnimationEnd(Animation animation) {

            int displayed = viewFlipper.getDisplayedChild();

            System.out.println("zoot >" + displayed + " Count >" + viewFlipper.getChildCount());

            for (int i = 0; i < mDotsCount; i++) {
                One.Points[i].setImageResource(R.drawable.hide);
            }

            One.Points[displayed].setImageResource(R.drawable.clip);

        }
    });



}

public boolean onTouchEvent(MotionEvent touchevent)
{
    switch (touchevent.getAction())
    {
        // when user first touches the screen to swap
        case MotionEvent.ACTION_DOWN:
        {
            lastX = touchevent.getX();
            break;
        }
        case MotionEvent.ACTION_UP:
        {
            float currentX = touchevent.getX();

            // if left to right swipe on screen
            if (lastX < currentX)
            {
                // If no more View/Child to flip
                if (viewFlipper.getDisplayedChild() == 0)
                    break;

                viewFlipper.setInAnimation(this, R.anim.in_from_left);
                viewFlipper.setOutAnimation(this, R.anim.out_to_right);
                // Show the next Screen
                viewFlipper.showNext();
            }

            if (lastX > currentX)
            {
                if (viewFlipper.getDisplayedChild() == 1)
                    break;

                viewFlipper.setInAnimation(this, R.anim.in_from_right);
                viewFlipper.setOutAnimation(this, R.anim.out_to_left);
                viewFlipper.showPrevious();
            }
            break;
        }
    }
    return false;
}

}

这里出现问题:当我设置动画来移动图像时,我收到与行“ viewFlipper.getAnimation()。setAnimationListener(new Animation.AnimationListener(){”)相关的NPE。我的第一个猜测是由于我在其他位置设置了动画侦听器以在切换视图时设置动画。还有其他想法吗?

Logcat的重点是:

E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: Process: xxxxxx, PID: 31808
E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{xxxxxxx.One}: java.lang.NullPointerException
E/AndroidRuntime:  Caused by: java.lang.NullPointerException
E/AndroidRuntime:     at xxxxxx.One.onCreate(One.java:64)

我已经正确设置了android:inAnimation和android:outAnimation还有什么呢?

android animation nullpointerexception viewflipper
1个回答
0
投票
很抱歉,显而易见,但是viewFlipper为null(这意味着它无法通过该id找到视图,可能是拼写错误吗?)或该视图没有动画,导致getAnimation()返回null。我将在该行上停止调试器,然后看看是哪个。应该很简单,您知道哪一个。
© www.soinside.com 2019 - 2024. All rights reserved.