Thursday, August 16, 2018

ViewPager in android Null pointer exception

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




android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


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

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


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

android:id="@android:id/tabcontent"

android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />

android:id="@+id/tabHostWithViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />






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

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...