0

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

DeNasti
  • 75
  • 1
  • 10
  • Please check the layout for setContentView is it the correct one R.layout.main_game – Meenal Jul 27 '17 at 09:17
  • i haven't noticed the inconsistency between the first and second code, but i've tryied to substitute "R.layout.main_game" with "R.layout.switch_game);" and nothing canged – DeNasti Jul 27 '17 at 09:40

1 Answers1

2

NullPointerException is thrown when an application attempts to use an object reference that has the null value.

A ViewFlipper is mainly used in cases when we need to transform one view into another.

  viewFlipper = (ViewFlipper) findViewById(R.id.switch_game);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    yes, sorry i have made a bit of confusion in the last part. The NullPointerException was thrown 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 – DeNasti Jul 27 '17 at 09:35