我有一个非常简单的活动(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;
}
}
问题出现了:当我设置动画来移动图像时,我会收到与行相关的NPE&#34; viewFlipper.getAnimation()。setAnimationListener(new Animation.AnimationListener(){&#34 ;. 我的第一个猜测是,由于我在其他位置设置动画侦听器以在移动视图时设置动画。 有什么进一步的想法吗?
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 还有什么呢?
答案 0 :(得分:0)
很抱歉说明显而易见,但是viewFlipper是null(这意味着它找不到该id的视图,可能是拼写错误?)或者该视图没有动画,导致getAnimation()返回null。我会停止该行的调试器,看看它是什么。你知道哪一个应该是相当简单的。
由于您在onTouch方法中设置动画,我的猜测是viewFlipper没有其他动画,所以当您创建活动时(还没有onTouch事件),getAnimation返回null。也许你想在添加监听器之前设置动画?
Kaamel