我有一个带有以下XML注释的ViewFlipper,注意这都包含在RelativeLayout中
<ViewFlipper
android:id="@+id/flipperIdentView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/identification_face"/>
<include layout="@layout/identification_rfid"/>
<include layout="@layout/identification_voice"/>
<include layout="@layout/identification_pill_dispense"/>
</ViewFlipper>
<LinearLayout
android:id="@+id/linear_button_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical" >
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/test_buttons"
android:gravity="center" />
</LinearLayout>
LinearLayout的ID为“linear_button_view”。现在在我的片段中,我有代码来确定翻转,并且一切正常。问题是,当我到达鳍状肢的索引3时,我想要隐藏这个布局。我应该做的就是使用此ID创建一个字段,然后将ViewVisibility设置为View.Gone。但是当我尝试这样做时,我正在抛出NPE。
在课堂上我有一个:
private View buttonView;
在我的onCreateView中,我有以下代码
View v = inflater.inflate(R.layout.pilltransferflipper, container, false);
buttonView = v.findViewById(R.id.linear_button_view);
这是我控制鳍状肢的方法,负责隐藏视图。
private void nextState() {
flipperIndex++;
if (flipperIndex >= flipperPillTransfer.getChildCount()) {
MainActivity activity = (MainActivity) getActivity();
activity.stateChange(State.SESSIONFINISHED);
} else {
flipperPillTransfer.showNext();
Log.d(TAG, "FLIPPER INDEX: " + flipperIndex);
if(flipperIndex == 3) {
buttonView.setVisibility(View.GONE);
}
}
}
当我在flipperIndex条件中放入一条Log.d消息时,它返回正常,似乎问题是buttonView.setVisibility的一部分。请记住,我正在使用片段,因此主要活动中可用的某些方法可能无法使用。谢谢!
09-26 10:05:54.381: D/PillTransferFragment(1719): FLIPPER INDEX: 3
09-26 10:05:54.381: D/AndroidRuntime(1719): Shutting down VM
09-26 10:05:54.381: W/dalvikvm(1719): threadid=1: thread exiting with uncaught exception (group=0x40c2b300)
09-26 10:05:54.381: E/AndroidRuntime(1719): FATAL EXCEPTION: main
09-26 10:05:54.381: E/AndroidRuntime(1719): java.lang.NullPointerException
09-26 10:05:54.381: E/AndroidRuntime(1719): at com.example.android.fragments.PillTransferFragment.nextState(PillTransferFragment.java:107)
09-26 10:05:54.381: E/AndroidRuntime(1719): at com.example.android.fragments.PillTransferFragment.onClick(PillTransferFragment.java:64)
09-26 10:05:54.381: E/AndroidRuntime(1719): at android.view.View.performClick(View.java:4084)