android - Invoking new activity class when a tab is clicked in actionbarsherlock -
folks,
i new android development question basic - using tabbed pane actionbarsherlock , want have 3 different ui 3 tabs. need 1 textbox in common across tabs. achieve first thing idea have 3 different activity classes extending actionbar.tablistener instantiating classes (using new) settablistener not working. solution?
regards,
the main activity class -
actionbar.tab atab = getsupportactionbar().newtab(); atab.settext("a"); atab.settablistener(new aactivity()); getsupportactionbar().addtab(atab); actionbar.tab btab = getsupportactionbar().newtab(); btab.settext("b"); btab.settablistener(new messageactivity()); getsupportactionbar().addtab(btab); actionbar.tab ctab = getsupportactionbar().newtab(); ctab .settext("c"); ctab .settablistener(new dataactivity()); getsupportactionbar().addtab(ctab );
now aactivity class -
public class aactivity extends activity implements actionbar.tablistener { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_call); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.call, menu); return true; } @override public void ontabselected(tab tab, fragmenttransaction ft) { system.out.println("in a"); textview txtview = (textview) findviewbyid(r.id.alog); } @override public void ontabunselected(tab tab, fragmenttransaction ft) { // todo auto-generated method stub } @override public void ontabreselected(tab tab, fragmenttransaction ft) { // todo auto-generated method stub } }
it giving me nullpointer @
textview txtview = (textview) findviewbyid(r.id.alog)
so assuming new thing not working @ all
you're doing wrong.
the tablistener
interface allows respond when tab selected or unselected. how respond you. doesn't matter if implementing class activity
or other particular class - long implements methods of tablistener
in way or another.
you should never have instantiate activity
class through new myactivity()
. android needs can set environment activity. thus, if want launch activity when tab selected, should calling startactivity
inside ontabselected
. however, launching activity when selecting tab bad idea. open separate activity (outside of original tabbed activity) , won't have tabbed action bar.
you're looking fragments: single activity container holding fragment can swap other fragments. when selecting tab, want replace current fragment fragment specific tab. demonstrated in actionbarsherlock samples, fragmenttabs.tabmanager
handles binding between tabs , fragments. use in code, should:
- make
mainactivity
extendsherlockfragmentactivity
. - grab layout
fragment_tabs.xml
. - grab
tabmanager
classfragmenttabs.java
, copy codebase (as separate class or static inner class, whatever works). copy setup code
fragmenttabs.oncreate
:super.oncreate(savedinstancestate); setcontentview(r.layout.fragment_tabs); mtabhost = (tabhost)findviewbyid(android.r.id.tabhost); mtabhost.setup(); mtabmanager = new tabmanager(this, mtabhost, r.id.realtabcontent);
create tabs
tabmanager.addtab
, example:mtabmanager.addtab(mtabhost.newtabspec("taba").setindicator("a"), afragment.class, null);
make
fragment
classes each tab (e.g.afragment
). overridefragment.oncreateview
, inflate ui , fancy stuff.
also, read fragments developer guide!
Comments
Post a Comment