How to set up tabs with view pager in android? -
i have followed following instructions in order sliding tabs view pager.
here oncreate method:
mviewpager.setadapter(new navigationpageradapter(getsupportfragmentmanager())); mtablayout.setupwithviewpager(mviewpager);
here navigation pager adapter:
public static class navigationpageradapter extends fragmentpageradapter { private static final int num_items = 3; public navigationpageradapter(fragmentmanager fragmentmanager) { super(fragmentmanager); } // returns total number of pages @override public int getcount() { return num_items; } // returns fragment display page @override public fragment getitem(int position) { switch (position) { case 0: // fragment # 0 - show firstfragment return new feedfragment(); default: return null; } } // returns page title top indicator @override public charsequence getpagetitle(int position) { return "page " + position; } }
here content_main.xml activity layout:
<android.support.design.widget.tablayout android:id="@+id/navigation_sliding_tabs" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabmode="scrollable"/> <android.support.v4.view.viewpager android:id="@+id/navigation_pager" android:layout_width="match_parent" android:layout_height="0px" android:layout_weight="1"/>
this error when running code:
java.lang.nullpointerexception: attempt write field 'android.support.v4.app.fragmentmanagerimpl android.support.v4.app.fragment.mfragmentmanager' on null object reference
i not sure if error lies in pager adapter seems fragment manager causing trouble.
try in adapter
feedfragment feedfragment; public navigationpageradapter(fragmentmanager fragmentmanager) { super(fragmentmanager); feedfragment=new feedfragment(); } // returns total number of pages @override public int getcount() { return num_items; } // returns fragment display page @override public fragment getitem(int position) { fragment fragment; switch (position) { case 0: // fragment # 0 - show firstfragment fragment=feedfragment; break; return fragment; } } // returns page title top indicator @override public charsequence getpagetitle(int position) { return "page " + position; } }
Comments
Post a Comment