Java – Use the FragmentPagerAdapter to programmatically change tabs from Fragment

Use the FragmentPagerAdapter to programmatically change tabs from Fragment… here is a solution to the problem.

Use the FragmentPagerAdapter to programmatically change tabs from Fragment

I’m trying to get my app to change from tab 1 to tab 3. These tabs are located in the custom TabsPagerAdapter, which extends the FragmentPagerAdapter.

I’ve tried changing the tab like this, but it results in a NullPointerException. Is the mechanism of FragmentPagerAdapter different?

TabHost host = (TabHost) getActivity().findViewById(android. R.id.tabhost);
host.setCurrentTab(2);

Solution

I see. Need to use my ViewPager instead of TabHost

ViewPager viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
viewPager.setCurrentItem(2);

Related Problems and Solutions