我有一个非常简单的活动(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还有什么呢?