我正在研究 android 中的 View Flipper。编译时没有显示任何错误。但是一旦应用程序在我的移动设备中启动,它就会崩溃。由于我是Android的初学者并且没有足够的知识。报告错误显示调用viewFlipper.getDisplayChild()
主要 java 文件代码的空指针异常是
package com.example.joshiyogesh.classapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ViewFlipper;
public class MainActivity extends Activity {
public ViewFlipper viewFlipper;
public float lastX;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewFlipper = (ViewFlipper)findViewById(R.id.view_flipper);
}
public boolean onTouchEvent(MotionEvent touchEvent){
switch (touchEvent.getAction()){
case MotionEvent.ACTION_DOWN:
{
lastX = touchEvent.getX();
break;
}
case MotionEvent.ACTION_UP:
{
float currentX = touchEvent.getX();
if (lastX<currentX){
if(viewFlipper.getDisplayedChild() == 0)
break;
viewFlipper.setInAnimation(this,R.anim.in_from_left);
viewFlipper.setOutAnimation(this,R.anim.out_from_right);
//show 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_from_left);
viewFlipper.showPrevious();
}
break;
}
}
return false;
}
}
ViewFlipper 的文件代码是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/view_flipper"
android:layout_margin="6dip">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
>
<ImageView
android:layout_width="450dp"
android:layout_height="450dp"
android:layout_marginTop="15dp"
android:id="@+id/image_view1"
android:src="@drawable/images"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
>
<ImageView
android:layout_width="450dp"
android:layout_height="450dp"
android:layout_marginTop="15dp"
android:id="@+id/image_view2"
android:src="@drawable/large"/>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
任何帮助将不胜感激。在此先感谢。
更新 设备显示的报告错误