android - getActionBar() returning null in FragmentActivity -
i trying build android tab layout swipeable views using this tutorial.
my fragmentactivity
:
public class bookdetailsactivity extends fragmentactivity implements actionbar.tablistener { private viewpager viewpager; private tabspageradapter madapter; private actionbar actionbar; // tab titles private string[] tabs = {"description", "find book"}; static arraylist<string> book; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); getwindow().requestfeature(window.feature_action_bar); setcontentview(r.layout.activity_books_details); book = new arraylist<string>(); book = (arraylist<string>) getintent().getserializableextra("bookmap"); // initilization viewpager = (viewpager) findviewbyid(r.id.pager); actionbar = getactionbar(); madapter = new tabspageradapter(getsupportfragmentmanager(), book, getapplicationcontext()); viewpager.setadapter(madapter); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); // adding tabs (string tab_name : tabs) { actionbar.addtab(actionbar.newtab().settext(tab_name) .settablistener(this)); } /** * on swiping viewpager make respective tab selected * */ viewpager.setonpagechangelistener(new viewpager.onpagechangelistener() { @override public void onpageselected(int position) { // on changing page // make respected tab selected actionbar.setselectednavigationitem(position); } @override public void onpagescrolled(int arg0, float arg1, int arg2) { } @override public void onpagescrollstatechanged(int arg0) { } }); } public static arraylist<string> getrequireddata() { return book; } @override public void ontabreselected(actionbar.tab tab, fragmenttransaction ft) { } @override public void ontabselected(actionbar.tab tab, fragmenttransaction ft) { // on tab selected // show respected fragment view viewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(actionbar.tab tab, fragmenttransaction ft) { } }
but getting error in:
actionbar.setnavigationmode(actionbar.navigation_mode_tabs);
the error says:
attempt invoke virtual method 'void android.app.actionbar.setnavigationmode(int)' on null object reference
why getting error?
android studio: actionbar.tablistener not work appcompatactivity?
actionbar.tablistener
deprecated interface
.i hope not use in future.
and, actionbartabs
can replaced by:
tablayout
android design support library
, or withoutviewpager
; ora
viewpager
, favorite other tab solution it, whetherpagertabstrip
or any number of third-party tab implementations; or
like said before in answer: https://stackoverflow.com/a/35203962/4409113
but fixing issue, that's because actionbar
not enabled right theme , extending fragmentactivity
.
please check link: attempt invoke virtual method 'void android.app.actionbar.setdisplayhomeasupenabled(boolean)' on null object reference
use appcompatactivity
instead add actionbar
, if used it, need too:
actionbar actionbar = getsupportactionbar();
Comments
Post a Comment