Same fragments in one TabLayout









up vote
0
down vote

favorite












I am trying to develop one activity, which contains TabLayout, which is build using one Fragment, but used a few times (same layout, different data). But when I have such a configuration, my app doesn't even go inside of getItem() method and doesn't show up my layout. Any ideas where is the problem?



My code:
Activity:



public class IndoorArenaActivity extends AppCompatActivity 
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.my_arena);

TabLayout tabLayout = findViewById(R.id.tabs);
final ViewPager viewPager = findViewById(R.id.viewPager);
final PageAdapterArena adapter = new PageAdapterArena
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

tabLayout.addTab(tabLayout.newTab().setText("today"));
tabLayout.addTab(tabLayout.newTab().setText("tomorrow"));
tabLayout.addTab(tabLayout.newTab().setText("13/11"));
tabLayout.addTab(tabLayout.newTab().setText("14/11"));
tabLayout.addTab(tabLayout.newTab().setText("15/11"));
tabLayout.addTab(tabLayout.newTab().setText("16/11"));
tabLayout.addTab(tabLayout.newTab().setText("17/11"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);


tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
@Override
public void onTabSelected(TabLayout.Tab tab)
viewPager.setCurrentItem(tab.getPosition());


@Override
public void onTabUnselected(TabLayout.Tab tab)



@Override
public void onTabReselected(TabLayout.Tab tab)


);


@Override
public boolean onCreateOptionsMenu(Menu menu)
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;


@Override
public boolean onOptionsItemSelected(MenuItem item)
int id = item.getItemId();
// if (id == R.id.action_settings)
// return true;
//

return super.onOptionsItemSelected(item);




Adapter:



public class PageAdapterArena extends FragmentStatePagerAdapter 
int mNumOfTabs;

public PageAdapterArena(FragmentManager fm, int NumOfTabs)
super(fm);
this.mNumOfTabs = NumOfTabs;


@Override
public Fragment getItem(int position)

Fragment fragment = null;
switch(position)
case 0:
fragment = IndoorArenaFragment.newInstance();
break;

return fragment;


@Override
public int getCount()
return mNumOfTabs;




Fragment:



public class IndoorArenaFragment extends Fragment 

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
return inflater.inflate(R.layout.indoor_arena_fragment, container, false);


public static Fragment newInstance()
IndoorArenaFragment fragment = new IndoorArenaFragment();
return fragment;




Layout for activity:



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".IndoorArena.IndoorArenaActivity">

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
app:tabMode="scrollable"/>

<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout" />




Layout for fragment:



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="7:00"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Olga (Gatsby)"
android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="8:00"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="9:00"
android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


App doesn't show up any errors, only the fragment looks like this:
enter image description here



And I would like to show up like this (each fragment):



enter image description here










share|improve this question

























    up vote
    0
    down vote

    favorite












    I am trying to develop one activity, which contains TabLayout, which is build using one Fragment, but used a few times (same layout, different data). But when I have such a configuration, my app doesn't even go inside of getItem() method and doesn't show up my layout. Any ideas where is the problem?



    My code:
    Activity:



    public class IndoorArenaActivity extends AppCompatActivity 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_arena);

    TabLayout tabLayout = findViewById(R.id.tabs);
    final ViewPager viewPager = findViewById(R.id.viewPager);
    final PageAdapterArena adapter = new PageAdapterArena
    (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.addTab(tabLayout.newTab().setText("today"));
    tabLayout.addTab(tabLayout.newTab().setText("tomorrow"));
    tabLayout.addTab(tabLayout.newTab().setText("13/11"));
    tabLayout.addTab(tabLayout.newTab().setText("14/11"));
    tabLayout.addTab(tabLayout.newTab().setText("15/11"));
    tabLayout.addTab(tabLayout.newTab().setText("16/11"));
    tabLayout.addTab(tabLayout.newTab().setText("17/11"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);


    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
    @Override
    public void onTabSelected(TabLayout.Tab tab)
    viewPager.setCurrentItem(tab.getPosition());


    @Override
    public void onTabUnselected(TabLayout.Tab tab)



    @Override
    public void onTabReselected(TabLayout.Tab tab)


    );


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;


    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    int id = item.getItemId();
    // if (id == R.id.action_settings)
    // return true;
    //

    return super.onOptionsItemSelected(item);




    Adapter:



    public class PageAdapterArena extends FragmentStatePagerAdapter 
    int mNumOfTabs;

    public PageAdapterArena(FragmentManager fm, int NumOfTabs)
    super(fm);
    this.mNumOfTabs = NumOfTabs;


    @Override
    public Fragment getItem(int position)

    Fragment fragment = null;
    switch(position)
    case 0:
    fragment = IndoorArenaFragment.newInstance();
    break;

    return fragment;


    @Override
    public int getCount()
    return mNumOfTabs;




    Fragment:



    public class IndoorArenaFragment extends Fragment 

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    return inflater.inflate(R.layout.indoor_arena_fragment, container, false);


    public static Fragment newInstance()
    IndoorArenaFragment fragment = new IndoorArenaFragment();
    return fragment;




    Layout for activity:



    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".IndoorArena.IndoorArenaActivity">

    <android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    app:tabMode="scrollable"/>

    <android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout" />




    Layout for fragment:



    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="20dp"
    android:text="7:00"
    android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:text="Olga (Gatsby)"
    android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>


    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="20dp"
    android:text="8:00"
    android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="20dp"
    android:text="9:00"
    android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>


    App doesn't show up any errors, only the fragment looks like this:
    enter image description here



    And I would like to show up like this (each fragment):



    enter image description here










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am trying to develop one activity, which contains TabLayout, which is build using one Fragment, but used a few times (same layout, different data). But when I have such a configuration, my app doesn't even go inside of getItem() method and doesn't show up my layout. Any ideas where is the problem?



      My code:
      Activity:



      public class IndoorArenaActivity extends AppCompatActivity 
      @Override
      protected void onCreate(Bundle savedInstanceState)
      super.onCreate(savedInstanceState);
      setContentView(R.layout.my_arena);

      TabLayout tabLayout = findViewById(R.id.tabs);
      final ViewPager viewPager = findViewById(R.id.viewPager);
      final PageAdapterArena adapter = new PageAdapterArena
      (getSupportFragmentManager(), tabLayout.getTabCount());
      viewPager.setAdapter(adapter);
      viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

      tabLayout.addTab(tabLayout.newTab().setText("today"));
      tabLayout.addTab(tabLayout.newTab().setText("tomorrow"));
      tabLayout.addTab(tabLayout.newTab().setText("13/11"));
      tabLayout.addTab(tabLayout.newTab().setText("14/11"));
      tabLayout.addTab(tabLayout.newTab().setText("15/11"));
      tabLayout.addTab(tabLayout.newTab().setText("16/11"));
      tabLayout.addTab(tabLayout.newTab().setText("17/11"));
      tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);


      tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
      @Override
      public void onTabSelected(TabLayout.Tab tab)
      viewPager.setCurrentItem(tab.getPosition());


      @Override
      public void onTabUnselected(TabLayout.Tab tab)



      @Override
      public void onTabReselected(TabLayout.Tab tab)


      );


      @Override
      public boolean onCreateOptionsMenu(Menu menu)
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;


      @Override
      public boolean onOptionsItemSelected(MenuItem item)
      int id = item.getItemId();
      // if (id == R.id.action_settings)
      // return true;
      //

      return super.onOptionsItemSelected(item);




      Adapter:



      public class PageAdapterArena extends FragmentStatePagerAdapter 
      int mNumOfTabs;

      public PageAdapterArena(FragmentManager fm, int NumOfTabs)
      super(fm);
      this.mNumOfTabs = NumOfTabs;


      @Override
      public Fragment getItem(int position)

      Fragment fragment = null;
      switch(position)
      case 0:
      fragment = IndoorArenaFragment.newInstance();
      break;

      return fragment;


      @Override
      public int getCount()
      return mNumOfTabs;




      Fragment:



      public class IndoorArenaFragment extends Fragment 

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
      return inflater.inflate(R.layout.indoor_arena_fragment, container, false);


      public static Fragment newInstance()
      IndoorArenaFragment fragment = new IndoorArenaFragment();
      return fragment;




      Layout for activity:



      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/main_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".IndoorArena.IndoorArenaActivity">

      <android.support.design.widget.TabLayout
      android:id="@+id/tabs"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="?attr/colorPrimary"
      android:elevation="6dp"
      android:minHeight="?attr/actionBarSize"
      app:tabMode="scrollable"/>

      <android.support.v4.view.ViewPager
      android:id="@+id/viewPager"
      android:layout_width="match_parent"
      android:layout_height="fill_parent"
      android:layout_below="@id/tab_layout" />




      Layout for fragment:



      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">

      <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">

      <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="20dp"
      android:text="7:00"
      android:textAppearance="?android:attr/textAppearanceLarge" />

      <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="50dp"
      android:text="Olga (Gatsby)"
      android:textAppearance="?android:attr/textAppearanceLarge" />

      </LinearLayout>


      <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="20dp"
      android:text="8:00"
      android:textAppearance="?android:attr/textAppearanceLarge" />

      <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="20dp"
      android:text="9:00"
      android:textAppearance="?android:attr/textAppearanceLarge" />

      </LinearLayout>


      App doesn't show up any errors, only the fragment looks like this:
      enter image description here



      And I would like to show up like this (each fragment):



      enter image description here










      share|improve this question













      I am trying to develop one activity, which contains TabLayout, which is build using one Fragment, but used a few times (same layout, different data). But when I have such a configuration, my app doesn't even go inside of getItem() method and doesn't show up my layout. Any ideas where is the problem?



      My code:
      Activity:



      public class IndoorArenaActivity extends AppCompatActivity 
      @Override
      protected void onCreate(Bundle savedInstanceState)
      super.onCreate(savedInstanceState);
      setContentView(R.layout.my_arena);

      TabLayout tabLayout = findViewById(R.id.tabs);
      final ViewPager viewPager = findViewById(R.id.viewPager);
      final PageAdapterArena adapter = new PageAdapterArena
      (getSupportFragmentManager(), tabLayout.getTabCount());
      viewPager.setAdapter(adapter);
      viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

      tabLayout.addTab(tabLayout.newTab().setText("today"));
      tabLayout.addTab(tabLayout.newTab().setText("tomorrow"));
      tabLayout.addTab(tabLayout.newTab().setText("13/11"));
      tabLayout.addTab(tabLayout.newTab().setText("14/11"));
      tabLayout.addTab(tabLayout.newTab().setText("15/11"));
      tabLayout.addTab(tabLayout.newTab().setText("16/11"));
      tabLayout.addTab(tabLayout.newTab().setText("17/11"));
      tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);


      tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
      @Override
      public void onTabSelected(TabLayout.Tab tab)
      viewPager.setCurrentItem(tab.getPosition());


      @Override
      public void onTabUnselected(TabLayout.Tab tab)



      @Override
      public void onTabReselected(TabLayout.Tab tab)


      );


      @Override
      public boolean onCreateOptionsMenu(Menu menu)
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;


      @Override
      public boolean onOptionsItemSelected(MenuItem item)
      int id = item.getItemId();
      // if (id == R.id.action_settings)
      // return true;
      //

      return super.onOptionsItemSelected(item);




      Adapter:



      public class PageAdapterArena extends FragmentStatePagerAdapter 
      int mNumOfTabs;

      public PageAdapterArena(FragmentManager fm, int NumOfTabs)
      super(fm);
      this.mNumOfTabs = NumOfTabs;


      @Override
      public Fragment getItem(int position)

      Fragment fragment = null;
      switch(position)
      case 0:
      fragment = IndoorArenaFragment.newInstance();
      break;

      return fragment;


      @Override
      public int getCount()
      return mNumOfTabs;




      Fragment:



      public class IndoorArenaFragment extends Fragment 

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
      return inflater.inflate(R.layout.indoor_arena_fragment, container, false);


      public static Fragment newInstance()
      IndoorArenaFragment fragment = new IndoorArenaFragment();
      return fragment;




      Layout for activity:



      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/main_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".IndoorArena.IndoorArenaActivity">

      <android.support.design.widget.TabLayout
      android:id="@+id/tabs"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="?attr/colorPrimary"
      android:elevation="6dp"
      android:minHeight="?attr/actionBarSize"
      app:tabMode="scrollable"/>

      <android.support.v4.view.ViewPager
      android:id="@+id/viewPager"
      android:layout_width="match_parent"
      android:layout_height="fill_parent"
      android:layout_below="@id/tab_layout" />




      Layout for fragment:



      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">

      <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">

      <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="20dp"
      android:text="7:00"
      android:textAppearance="?android:attr/textAppearanceLarge" />

      <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="50dp"
      android:text="Olga (Gatsby)"
      android:textAppearance="?android:attr/textAppearanceLarge" />

      </LinearLayout>


      <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="20dp"
      android:text="8:00"
      android:textAppearance="?android:attr/textAppearanceLarge" />

      <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="20dp"
      android:text="9:00"
      android:textAppearance="?android:attr/textAppearanceLarge" />

      </LinearLayout>


      App doesn't show up any errors, only the fragment looks like this:
      enter image description here



      And I would like to show up like this (each fragment):



      enter image description here







      android android-fragments android-tablayout






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 21:28









      gariaable

      578




      578






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You're actually passing in NumOfTabs of 0 when you create the adapter, that's why you're getting an empty view in the ViewPager, and getItem not being called. To fix this, you just need to move your adapter creation line after adding the tabs:



          TabLayout tabLayout = findViewById(R.id.tabs);
          ViewPager viewPager = findViewById(R.id.viewPager);
          tabLayout.addTab(tabLayout.newTab().setText("today"));
          tabLayout.addTab(tabLayout.newTab().setText("tomorrow"));
          tabLayout.addTab(tabLayout.newTab().setText("13/11"));
          tabLayout.addTab(tabLayout.newTab().setText("14/11"));
          tabLayout.addTab(tabLayout.newTab().setText("15/11"));
          tabLayout.addTab(tabLayout.newTab().setText("16/11"));
          tabLayout.addTab(tabLayout.newTab().setText("17/11"));
          tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

          // Create adapter after adding the tabs
          PageAdapterArena adapter = new PageAdapterArena(getSupportFragmentManager(), tabLayout.getTabCount());
          viewPager.setAdapter(adapter);
          viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));





          share|improve this answer




















          • Thanks! But when I do that I get an error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:123)
            – gariaable
            Nov 12 at 0:22







          • 1




            You need to fix your getItem in your adapter. Currently it's only returning fragment if requested position is 0, but null for other positions. Do not return null on getItem.
            – Aaron
            Nov 12 at 0:26











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253410%2fsame-fragments-in-one-tablayout%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          You're actually passing in NumOfTabs of 0 when you create the adapter, that's why you're getting an empty view in the ViewPager, and getItem not being called. To fix this, you just need to move your adapter creation line after adding the tabs:



          TabLayout tabLayout = findViewById(R.id.tabs);
          ViewPager viewPager = findViewById(R.id.viewPager);
          tabLayout.addTab(tabLayout.newTab().setText("today"));
          tabLayout.addTab(tabLayout.newTab().setText("tomorrow"));
          tabLayout.addTab(tabLayout.newTab().setText("13/11"));
          tabLayout.addTab(tabLayout.newTab().setText("14/11"));
          tabLayout.addTab(tabLayout.newTab().setText("15/11"));
          tabLayout.addTab(tabLayout.newTab().setText("16/11"));
          tabLayout.addTab(tabLayout.newTab().setText("17/11"));
          tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

          // Create adapter after adding the tabs
          PageAdapterArena adapter = new PageAdapterArena(getSupportFragmentManager(), tabLayout.getTabCount());
          viewPager.setAdapter(adapter);
          viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));





          share|improve this answer




















          • Thanks! But when I do that I get an error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:123)
            – gariaable
            Nov 12 at 0:22







          • 1




            You need to fix your getItem in your adapter. Currently it's only returning fragment if requested position is 0, but null for other positions. Do not return null on getItem.
            – Aaron
            Nov 12 at 0:26















          up vote
          1
          down vote



          accepted










          You're actually passing in NumOfTabs of 0 when you create the adapter, that's why you're getting an empty view in the ViewPager, and getItem not being called. To fix this, you just need to move your adapter creation line after adding the tabs:



          TabLayout tabLayout = findViewById(R.id.tabs);
          ViewPager viewPager = findViewById(R.id.viewPager);
          tabLayout.addTab(tabLayout.newTab().setText("today"));
          tabLayout.addTab(tabLayout.newTab().setText("tomorrow"));
          tabLayout.addTab(tabLayout.newTab().setText("13/11"));
          tabLayout.addTab(tabLayout.newTab().setText("14/11"));
          tabLayout.addTab(tabLayout.newTab().setText("15/11"));
          tabLayout.addTab(tabLayout.newTab().setText("16/11"));
          tabLayout.addTab(tabLayout.newTab().setText("17/11"));
          tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

          // Create adapter after adding the tabs
          PageAdapterArena adapter = new PageAdapterArena(getSupportFragmentManager(), tabLayout.getTabCount());
          viewPager.setAdapter(adapter);
          viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));





          share|improve this answer




















          • Thanks! But when I do that I get an error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:123)
            – gariaable
            Nov 12 at 0:22







          • 1




            You need to fix your getItem in your adapter. Currently it's only returning fragment if requested position is 0, but null for other positions. Do not return null on getItem.
            – Aaron
            Nov 12 at 0:26













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You're actually passing in NumOfTabs of 0 when you create the adapter, that's why you're getting an empty view in the ViewPager, and getItem not being called. To fix this, you just need to move your adapter creation line after adding the tabs:



          TabLayout tabLayout = findViewById(R.id.tabs);
          ViewPager viewPager = findViewById(R.id.viewPager);
          tabLayout.addTab(tabLayout.newTab().setText("today"));
          tabLayout.addTab(tabLayout.newTab().setText("tomorrow"));
          tabLayout.addTab(tabLayout.newTab().setText("13/11"));
          tabLayout.addTab(tabLayout.newTab().setText("14/11"));
          tabLayout.addTab(tabLayout.newTab().setText("15/11"));
          tabLayout.addTab(tabLayout.newTab().setText("16/11"));
          tabLayout.addTab(tabLayout.newTab().setText("17/11"));
          tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

          // Create adapter after adding the tabs
          PageAdapterArena adapter = new PageAdapterArena(getSupportFragmentManager(), tabLayout.getTabCount());
          viewPager.setAdapter(adapter);
          viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));





          share|improve this answer












          You're actually passing in NumOfTabs of 0 when you create the adapter, that's why you're getting an empty view in the ViewPager, and getItem not being called. To fix this, you just need to move your adapter creation line after adding the tabs:



          TabLayout tabLayout = findViewById(R.id.tabs);
          ViewPager viewPager = findViewById(R.id.viewPager);
          tabLayout.addTab(tabLayout.newTab().setText("today"));
          tabLayout.addTab(tabLayout.newTab().setText("tomorrow"));
          tabLayout.addTab(tabLayout.newTab().setText("13/11"));
          tabLayout.addTab(tabLayout.newTab().setText("14/11"));
          tabLayout.addTab(tabLayout.newTab().setText("15/11"));
          tabLayout.addTab(tabLayout.newTab().setText("16/11"));
          tabLayout.addTab(tabLayout.newTab().setText("17/11"));
          tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

          // Create adapter after adding the tabs
          PageAdapterArena adapter = new PageAdapterArena(getSupportFragmentManager(), tabLayout.getTabCount());
          viewPager.setAdapter(adapter);
          viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 22:33









          Aaron

          1,6951212




          1,6951212











          • Thanks! But when I do that I get an error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:123)
            – gariaable
            Nov 12 at 0:22







          • 1




            You need to fix your getItem in your adapter. Currently it's only returning fragment if requested position is 0, but null for other positions. Do not return null on getItem.
            – Aaron
            Nov 12 at 0:26

















          • Thanks! But when I do that I get an error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:123)
            – gariaable
            Nov 12 at 0:22







          • 1




            You need to fix your getItem in your adapter. Currently it's only returning fragment if requested position is 0, but null for other positions. Do not return null on getItem.
            – Aaron
            Nov 12 at 0:26
















          Thanks! But when I do that I get an error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:123)
          – gariaable
          Nov 12 at 0:22





          Thanks! But when I do that I get an error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:123)
          – gariaable
          Nov 12 at 0:22





          1




          1




          You need to fix your getItem in your adapter. Currently it's only returning fragment if requested position is 0, but null for other positions. Do not return null on getItem.
          – Aaron
          Nov 12 at 0:26





          You need to fix your getItem in your adapter. Currently it's only returning fragment if requested position is 0, but null for other positions. Do not return null on getItem.
          – Aaron
          Nov 12 at 0:26


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253410%2fsame-fragments-in-one-tablayout%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto