I have a UI
with multiple views that show sets of buttons to the user. I use a viewflipper
to manage flipping back and forth between the button sets. Recently a new error message in Android Studio showed up:
"Method invocation 'viewflipper.getDisplayedChild()' may produce 'java.lang.NullPointerException'..."
The error shows on ** **line in the Activity file:
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cardviewinput);
viewflipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
**if (viewflipper.getDisplayedChild() == 0)**
viewflipper.setDisplayedChild(1);
viewflipper.setDisplayedChild(viewflipper.indexOfChild(findViewById(R.id.cardviewNobuttons)));
"cardviewNobuttons"
is the default layout I want to show.
In cardviewinput.xml file:
...
<LinearLayout
android:id="@+id/LinearLayout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical" >
<ViewFlipper
android:id="@+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/cardview_nobuttons"
android:id="@+id/cardviewNobuttons" />
<include layout="@layout/cardview_previewbuttons"
android:id="@+id/cardviewPreviewbuttons" />
<include layout="@layout/cardview_twobuttons"
android:id="@+id/cardviewTwobuttons" />
</ViewFlipper>
</LinearLayout>
The warning in the above layout file is "The ViewFlipper layout or its LinearLayout parent is possibly useless..."
Is there a way to improve the code and negate the error messages/warnings?