In my android apps i have 2 Layouts created using viewFlipper
<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:id="@+id/switching"
android:layout_height="match_parent">
<include layout="@layout/activity_login"/>
<include layout="@layout/content_team_choose"/>
<include layout="@layout/waiting"/>
</ViewFlipper>
and
<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:id="@+id/switch_game"
android:layout_height="match_parent">
<include layout="@layout/main_game"/>
<include layout="@layout/chat"/>
</ViewFlipper>
in my fist activity have access to the view like this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.switching );
viewFlipper = (ViewFlipper) findViewById(R.id.switching);
//some other code which triggers the change of the view,
//like a onClickListener
viewFlipper.setDisplayedChild(1);
and this works fine. In my second activity, if i try to access to the other view flipper, and i do vievFlipper.setDisplayedChild(1) a NullPointerExcption is thrown. So i tried this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.switch_game);
viewFlipper = (ViewFlipper) findViewById(R.id.switch_game);
if(viewFipper == null)
Log.d("ViewError", "the viewFlipper is null");
and the Log appears, but i can't get why only in this case the view is null. I have even tryied to get the Layout "switching" in the second activity and this too gave me the same issues.
EDIT. The NullPointerException was thrown by a viewFlipper.setDisplayedChild(1); trying to access to a children of the layout, but i wasn't sure if only the childrens were null or all the viewFlipper, so i tryied the last part of code, verifying that the whole layout was null