android - How to add sliding tabs on an existing navigation bar? -
i have navigation drawer on current application. however, seems i'm having more modules coming in, have make sure application still organized. thinking add sliding tab on 1 of navigation drawer fragment.
during attempt, went wrong , log shows following error
nullpointerexception: attempt invoke virtual method 'boolean android.content.intent.migrateextrastreamtoclipdata()' on null object reference
i tried googling issue no success in getting solution. can tell me wrong code?
my following files are.
mainactivity.java
package com.epsilon.rfcalculator; import android.content.intent; import android.content.res.configuration; import android.os.bundle; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbaractivity; import android.support.v7.app.actionbardrawertoggle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.listview; public class mainactivity extends actionbaractivity { private listview mdrawerlist; private drawerlayout mdrawerlayout; private arrayadapter<string> madapter; private actionbardrawertoggle mdrawertoggle; private string mactivitytitle; private myfragment fragment; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mdrawerlist = (listview) findviewbyid(r.id.navlist); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mactivitytitle = gettitle().tostring(); adddraweritems(); setupdrawer(); getsupportactionbar().setdisplayhomeasupenabled(true); getsupportactionbar().sethomebuttonenabled(true); } private void adddraweritems() { string[] osarray = {"free space propagation model", "two ray ground propagation model", "power conversion", "wavelength conversion", "link budget"}; madapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, osarray); mdrawerlist.setadapter(madapter); mdrawerlist.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { //toast.maketext(mainactivity.this, "time upgrade!", toast.length_short).show(); displayview(position); } }); } private void displayview(int position) { // update main content replacing fragments intent intent = null; switch (position) { case 0: fragment = new myfragment(); break; //intent = new intent(this, freespaceactivity.class); //break; case 1: intent = new intent(this, transmissionactivity.class); break; case 2: intent = new intent(this, poweractivity.class); break; case 3: intent = new intent(this, wavelengthactivity.class); break; case 4: intent = new intent(this, linkbudgetactivity.class); break; //case 5: intent = new intent(this,mapactivity.class);break; } startactivity(intent); } private void setupdrawer() { mdrawertoggle = new actionbardrawertoggle(this, mdrawerlayout, r.string.drawer_open, r.string.drawer_close) { /** called when drawer has settled in open state. */ public void ondraweropened(view drawerview) { super.ondraweropened(drawerview); getsupportactionbar().settitle("menu"); invalidateoptionsmenu(); // creates call onprepareoptionsmenu() } /** called when drawer has settled in closed state. */ public void ondrawerclosed(view view) { super.ondrawerclosed(view); getsupportactionbar().settitle(mactivitytitle); invalidateoptionsmenu(); // creates call onprepareoptionsmenu() } }; mdrawertoggle.setdrawerindicatorenabled(true); mdrawerlayout.setdrawerlistener(mdrawertoggle); } @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); // sync toggle state after onrestoreinstancestate has occurred. mdrawertoggle.syncstate(); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); mdrawertoggle.onconfigurationchanged(newconfig); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } // activate navigation drawer toggle if (mdrawertoggle.onoptionsitemselected(item)) { return true; } return super.onoptionsitemselected(item); } }
myfragment.java
//this new java file added corresponding attempt add sliding tab.
package com.epsilon.rfcalculator; import android.content.context; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmentpageradapter; import android.support.v4.view.viewpager; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.tabhost; import android.widget.tabwidget; import java.util.arraylist; public class myfragment extends fragment { private tabhost mtabhost; private viewpager mviewpager; private tabsadapter mtabsadapter; public myfragment() { } @override public void oncreate(bundle instance) { super.oncreate(instance); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.fragment_main, container, false); mtabhost = (tabhost) v.findviewbyid(android.r.id.tabhost); mtabhost.setup(); mviewpager = (viewpager) v.findviewbyid(r.id.pager); mtabsadapter = new tabsadapter(getactivity(), mtabhost, mviewpager); // here load content each tab. mtabsadapter.addtab(mtabhost.newtabspec("one").setindicator("one"), freespaceactivity.class, null); mtabsadapter.addtab(mtabhost.newtabspec("two").setindicator("two"), transmissionactivity.class, null); return v; } public static class tabsadapter extends fragmentpageradapter implements tabhost.ontabchangelistener, viewpager.onpagechangelistener { private final context mcontext; private final tabhost mtabhost; private final viewpager mviewpager; private final arraylist<tabinfo> mtabs = new arraylist<tabinfo>(); static final class tabinfo { private final string tag; private final class<?> clss; private final bundle args; tabinfo(string _tag, class<?> _class, bundle _args) { tag = _tag; clss = _class; args = _args; } } static class dummytabfactory implements tabhost.tabcontentfactory { private final context mcontext; public dummytabfactory(context context) { mcontext = context; } public view createtabcontent(string tag) { view v = new view(mcontext); v.setminimumwidth(0); v.setminimumheight(0); return v; } } public tabsadapter(fragmentactivity activity, tabhost tabhost, viewpager pager) { super(activity.getsupportfragmentmanager()); mcontext = activity; mtabhost = tabhost; mviewpager = pager; mtabhost.setontabchangedlistener(this); mviewpager.setadapter(this); mviewpager.setonpagechangelistener(this); } public void addtab(tabhost.tabspec tabspec, class<?> clss, bundle args) { tabspec.setcontent(new dummytabfactory(mcontext)); string tag = tabspec.gettag(); tabinfo info = new tabinfo(tag, clss, args); mtabs.add(info); mtabhost.addtab(tabspec); notifydatasetchanged(); } @override public int getcount() { return mtabs.size(); } @override public fragment getitem(int position) { tabinfo info = mtabs.get(position); return fragment.instantiate(mcontext, info.clss.getname(), info.args); } public void ontabchanged(string tabid) { int position = mtabhost.getcurrenttab(); mviewpager.setcurrentitem(position); } public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { } public void onpageselected(int position) { // unfortunately when tabhost changes current tab, kindly // takes care of putting focus on when not in touch mode. // jerk. // hack tries prevent pulling focus out of our // viewpager. tabwidget widget = mtabhost.gettabwidget(); int oldfocusability = widget.getdescendantfocusability(); widget.setdescendantfocusability(viewgroup.focus_block_descendants); mtabhost.setcurrenttab(position); widget.setdescendantfocusability(oldfocusability); } public void onpagescrollstatechanged(int state) { } } }
activity_main.xml
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- first child in layout main activity ui--> <relativelayout android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity" android:background="#ffffffff"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:text="holy operating systems, batdroid!" android:textsize="24sp" android:gravity="center" android:layout_margintop="100dp"/> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true"/> </relativelayout> <!-- side navigation drawer ui --> <listview android:id="@+id/navlist" android:layout_width="200dp" android:layout_height="match_parent" android:layout_gravity="left|start" android:background="#ffeeeeee"/> </android.support.v4.widget.drawerlayout>
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <tabhost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <linearlayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <tabwidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0"/> <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/pager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> </linearlayout> </tabhost> </linearlayout>
Comments
Post a Comment