1

ViewPager gives null pointer exception.

I am trying to add tab and view pager inside fragment

Below is my code

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle       
savedInstanceState)    
{
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.activity_main_fragment_layout);
mViewPager = (ViewPager) mTabHost.findViewById(R.id.tabHostWithViewPager);

for (int i = 0; i < 5; ++i) {
mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Fragment B"), FragmentB.class,
null);
}
FragmentPagerAdapter fragmentPagerAdapter = new    
FragmentPageAdapter(getActivity().getSupportFragmentManager());

mViewPager.setAdapter(fragmentPagerAdapter);
    mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int arg0) {
            TabWidget widget = mTabHost.getTabWidget();
            int oldFocusability = widget.getDescendantFocusability();
            widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
            mTabHost.setCurrentTab(arg0);
            widget.setDescendantFocusability(oldFocusability);
            // actionBar.setSelectedNavigationItem(arg0);
        }

    });
    return mTabHost;
}

activity_main_fragment_layout.xml

<?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" >

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="horizontal" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />

        <android.support.v4.view.ViewPager
            android:id="@+id/tabHostWithViewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</TabHost>

</LinearLayout>

FragmentPagerAdapter.java

 public class FragmentPageAdapter extends FragmentPagerAdapter {
public FragmentPageAdapter(FragmentManager fm) {
    super(fm);
    // TODO Auto-generated constructor stub
}
@Override
public int getCount() {
    return 5;
}

@Override
public Fragment getItem(int arg0) {
    return new FragmentA();
}
}

Please help me to solve this issue.I can add tab in fragment but gets null pointer exception when trying to implement viewpager.Viewpager gives null pointer exception enter image description here

Please find out the logcat

backcross
  • 93
  • 1
  • 9

0 Answers0