-1

I have a set of tabs in android studio. They all work fine but non of them show any content, even though they do have content. So I did some digging around on this website and I found this Problems that users encounter with fragments

So I followed the advice the user gave. But it seems it like that my app crashes because of a null pointer exception. Can anybody help me spot the error please:
MainActivity.java

public class MainActivity extends AppCompatActivity implements Photos.OnFragmentInteractionListener, About.OnFragmentInteractionListener, Watch.OnFragmentInteractionListener{
    private TabLayout myTabLayOut;
    private  ViewPager viewPager;
    private  TabsPagerAdapter tabsPagerAdapter;
    public MainActivity()
    {

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_content);

        myTabLayOut = findViewById(R.id.tabLayout);
        myTabLayOut.addTab(myTabLayOut.newTab().setText("Photos"));
        myTabLayOut.addTab(myTabLayOut.newTab().setText("About"));
        myTabLayOut.addTab(myTabLayOut.newTab().setText("Watch"));
        myTabLayOut.setTabGravity(TabLayout.GRAVITY_FILL);


        myTabLayOut.addOnTabSelectedListener(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) {

            }
        });

        viewPager = findViewById(R.id.pager);
        //Item 0 being the photos fragment. Just for testing for the time being. WHERE The error happens
        tabsPagerAdapter = new TabsPagerAdapter(tabsPagerAdapter.getItem(0).getTargetFragment(), myTabLayOut.getTabCount());
        viewPager.setAdapter(tabsPagerAdapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(myTabLayOut));
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.my_menu,menu);
        return true;
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}

TabsPagerAdapter.java

public class TabsPagerAdapter extends FragmentPagerAdapter {
    //A field to hold the number of tabs
    private int numberOfTabs;
    public TabsPagerAdapter(Fragment fragmentManager, int numberOfTabs)
    {

        super(fragmentManager.getChildFragmentManager());
        this.numberOfTabs = numberOfTabs;
    }
    //Which item to return depending on the position
    @Override
    public Fragment getItem(int position) {
        switch (position)
        {
            case 0:
                return new Photos();
            case 1:
                return new About();
            case 2:
                return new Watch();
                default:
                    return null;
        }
    }

    @Override
    public int getCount() {
        return numberOfTabs;
    }
}
  • 2
    Voting to close two-fold. [How to fix NPE](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it), also, you havenot actually posted the error or NPE. – Matt Clark Jun 07 '18 at 22:50
  • Its a null pointer exception to item 0... Go back to your fidget spinner. – James Jhonson Jun 07 '18 at 22:57
  • 2
    Huh? Was that an attempted insult at people actually trying to help you? – Matt Clark Jun 07 '18 at 22:58
  • Oh no no no. Are you crazy? That was just merely a thank you for the likes of you :) – James Jhonson Jun 07 '18 at 23:01
  • `tabsPagerAdapter = new TabsPagerAdapter(tabsPagerAdapter.getItem(0)...` – `tabsPagerAdapter` is null there, so calling `getItem()` on it will throw an NPE. `PagerAdapter` does need a `FragmentManager`, but I don't know why you're trying to pass a `Fragment` to it, and then getting the (wrong) `FragmentManager` from that. Just pass the `Activity`'s `FragmentManager`. – Mike M. Jun 07 '18 at 23:03

2 Answers2

0

Your issue is with this line:

tabsPagerAdapter = new TabsPagerAdapter(tabsPagerAdapter.getItem(0)...

While attempting to assign the value of tabsPagerAdapter, you are attempting to use a value from this object which is at the time NULL.

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
0

FragmentViewPagers getItem method name is misleading - it does not return actual fragment presented in the pager, it's used only to create them once. If you try to call this method you will always get a new item.

Pawel
  • 15,548
  • 3
  • 36
  • 36