ViewFlipper.setDisplayedChild()在Android中无效

时间:2013-03-01 07:03:05

标签: android layout viewflipper

我有像这样的ViewFlipper布局,

   <ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flipper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
     <include android:id="@+id/mainView"  layout="@layout/activity_main" />
     <include android:id="@+id/readingView"  layout="@layout/get_reading" />
     <include android:id="@+id/datePickView" layout="@layout/custom_dialog" />
    </ViewFlipper>

并在MainActivity中

  public class MainActivity extends Activity {

  final Context context = this;
  public ViewFlipper flipper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);   
    flipper = (ViewFlipper) findViewById(R.id.flipper);
  //Inflate the Views      
    setContentView(R.layout.activity_main);
    Button AddWaterReading=(Button)findViewById(R.id.button1);    //click on + button.
    AddWaterReading.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            flipper.setDisplayedChild(R.id.readingView);
            TextView txtgetCurrentDate=(TextView)findViewById(R.id.editText1);
            final Calendar c=Calendar.getInstance();
            txtgetCurrentDate.setText((c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DATE)+"/"+c.get(Calendar.YEAR));                         
        }
    });
    }

当我点击“AddWaterReading”按钮时,布局视图没有更改为“readingView”。我使用ViewFlipper从Main布局更改为另一个布局。但是应用程序错误显示出来(“不幸的是应用程序停止了“)。

logcat的:

   03-01 07:29:58.122: E/Trace(3924): error opening trace file: No such file or   directory               (2)
   03-01 07:29:59.102: I/Choreographer(3924): Skipped 200 frames!  The application may   be doing too much work on its main thread.
   03-01 07:29:59.232: D/gralloc_goldfish(3924): Emulator without GPU emulation detected.
   03-01 07:30:07.972: D/AndroidRuntime(3924): Shutting down VM
   03-01 07:30:07.972: W/dalvikvm(3924): threadid=1: thread exiting with uncaught exception (group=0x2bd39930)
   03-01 07:30:07.982: E/AndroidRuntime(3924): FATAL EXCEPTION: main
   03-01 07:30:07.982: E/AndroidRuntime(3924): java.lang.NullPointerException
   03-01 07:30:07.982: E/AndroidRuntime(3924):  at com.example.electricityreading.MainActivity$1.onClick(MainActivity.java:35)
   03-01 07:30:07.982: E/AndroidRuntime(3924):  at android.view.View.performClick(View.java:4202)
   03-01 07:30:07.982: E/AndroidRuntime(3924):  at android.view.View$PerformClick.run(View.java:17340)
   03-01 07:30:07.982: E/AndroidRuntime(3924):  at android.os.Handler.handleCallback(Handler.java:725)
   03-01 07:30:07.982: E/AndroidRuntime(3924):  at android.os.Handler.dispatchMessage(Handler.java:92)
   03-01 07:30:07.982: E/AndroidRuntime(3924):  at android.os.Looper.loop(Looper.java:137)
  03-01 07:30:07.982: E/AndroidRuntime(3924):   at android.app.ActivityThread.main(ActivityThread.java:5039)
  03-01 07:30:07.982: E/AndroidRuntime(3924):   at java.lang.reflect.Method.invokeNative(Native Method)
  03-01 07:30:07.982: E/AndroidRuntime(3924):   at java.lang.reflect.Method.invoke(Method.java:511)
  03-01 07:30:07.982: E/AndroidRuntime(3924):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  03-01 07:30:07.982: E/AndroidRuntime(3924):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  03-01 07:30:07.982: E/AndroidRuntime(3924):   at dalvik.system.NativeStart.main(Native Method)

任何人都可以建议如何在布局之间切换?我是Android的新手。

感谢。

1 个答案:

答案 0 :(得分:1)

ViewFlipper的setDisplayedChild()方法会显示视图的INDEX,而非视图:http://developer.android.com/reference/android/widget/ViewAnimator.html#setDisplayedChild(int)