Android tabs fragment crashes when changing to landscape -
i have activity 3 tabs using fragments, works fine when working in portrait mode, however, when change landscape, crashes. code:
public class mainmenuactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main_menu); actionbar actionbar = getactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); string label1 = getresources().getstring(r.string.usermaintab); tab tab = actionbar.newtab(); tab.settext(label1); tablistener<usertabactivity> tl = new tablistener<usertabactivity>(this, label1, usertabactivity.class); tab.settablistener(tl); actionbar.addtab(tab); string label2 = getresources().getstring(r.string.credentialgroupmaintab); tab tab2 = actionbar.newtab(); tab2.settext(label2); tablistener<credentialgroupactivity> tl2 = new tablistener<credentialgroupactivity>(this, label2, credentialgroupactivity.class); tab2.settablistener(tl2); actionbar.addtab(tab2); string label3 = getresources().getstring(r.string.credentialtabheader); tab tab3 = actionbar.newtab(); tab3.settext(label3); tablistener<credentialtabactivity> tl3 = new tablistener<credentialtabactivity>(this, label2, credentialtabactivity.class); tab3.settablistener(tl3); actionbar.addtab(tab3); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main_menu, menu); return true; } public static class tablistener<t extends fragment> implements actionbar.tablistener{ private fragment mfragment; private final activity mactivity; private final string mtag; private final class<t> mclass; public tablistener(activity activity, string tag, class<t> clz){ mactivity = activity; mtag = tag; mclass = clz; mfragment = mactivity.getfragmentmanager().findfragmentbytag(mtag); if(mfragment != null && !mfragment.isdetached()){ fragmenttransaction ft = mactivity.getfragmentmanager().begintransaction(); ft.detach(mfragment); ft.commit(); } } @override public void ontabreselected(tab arg0, fragmenttransaction arg1) { // todo auto-generated method stub } @override public void ontabselected(tab arg0, fragmenttransaction arg1) { // todo auto-generated method stub if(mfragment == null){ mfragment = fragment.instantiate(mactivity, mclass.getname()); arg1.add(android.r.id.content, mfragment, mtag); } else{ arg1.attach(mfragment); } } @override public void ontabunselected(tab arg0, fragmenttransaction arg1) { // todo auto-generated method stub if(mfragment != null){ arg1.detach(mfragment); } } } } could tell me why not working? error is:
java.lang.runtimeexception: unable destroy activity{activity_name} : java.lang.nullpointerexception i have noticed fails until click on of tabs, , try change landscape, haven't been able identify in code what's happening, , also, when change view, loses tab positioned.
thanks in advanced!
edit
in order keep selected tab after changing orientation, ended doing this, suggestions made nathaniel waggoner , karthikk:
protected void onsaveinstancestate(bundle outstate) { // todo auto-generated method stub super.onsaveinstancestate(outstate); outstate.putint("tabstate", actionbar.getselectedtab().getposition()); } //in oncreate method if(savedinstancestate != null ){ actionbar.selecttab(actionbar.gettabat(savedinstancestate.getint("tabstate"))); } thanks!!
are overriding ondestroy in fragments? npe show symptom i've seen when i've overidden ondestroy in fragments used in tabs. remove override if , see if clears issue.
Comments
Post a Comment