android - getSupportActionBar() returns null with a custom toolbar -
i have toolbar. now, want navigation tabs in app. purpose, wrote following code:
super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar = (toolbar) findviewbyid(r.id.app_bar); toolbar.setlogo(r.mipmap.ic_launcher); // toolbar.settitle(" daily qura'n verses"); setsupportactionbar(toolbar); actionbar actionbar = getsupportactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); actionbar.setdisplayshowtitleenabled(false); actionbar.tab tab = actionbar.newtab() .settext("artistic fragment") .settablistener(new tablistener<artistfragment>( this, "artist", artistfragment.class)); actionbar.addtab(tab); tab = actionbar.newtab() .settext("album fragment") .settablistener(new tablistener<albumfragment>( this, "album", albumfragment.class)); actionbar.addtab(tab);
i have set no action bar in styles.xml:
<style name="apptheme" parent="theme.appcompat.light.noactionbar"> <!-- customize theme here. --> <item name="coloraccent">@color/accentcolor</item> <item name="colorprimary">@color/primarycolor</item> <item name="colorprimarydark">@color/primarycolordark</item> </style>
i have 2 fragments:
- albumfragment
- artistfragment
i have java class, tablistener:
package com.example.shiza.dailyquranverses; import android.app.activity; import android.support.v4.app.fragment; import android.support.v4.app.fragmenttransaction; import android.support.v7.app.actionbar; public class tablistener<t extends fragment> implements actionbar.tablistener { private fragment mfragment; private final activity mactivity; private final string mtag; private final class<t> mclass; /** constructor used each time new tab created. * @param activity host activity, used instantiate fragment * @param tag identifier tag fragment * @param clz fragment's class, used instantiate fragment */ public tablistener(activity activity, string tag, class<t> clz) { mactivity = activity; mtag = tag; mclass = clz; } /* following each of actionbar.tablistener callbacks */ public void ontabselected(actionbar.tab tab, fragmenttransaction ft) { // check if fragment initialized if (mfragment == null) { // if not, instantiate , add activity mfragment = fragment.instantiate(mactivity, mclass.getname()); ft.add(android.r.id.content, mfragment, mtag); } else { // if exists, attach in order show ft.attach(mfragment); } } public void ontabunselected(actionbar.tab tab, fragmenttransaction ft) { if (mfragment != null) { // detach fragment, because 1 being attached ft.detach(mfragment); } } public void ontabreselected(actionbar.tab tab, fragmenttransaction ft) { // user selected selected tab. nothing. } }
i think culprit following line:
actionbar.setnavigationmode(actionbar.navigation_mode_tabs);
because gives me warning.
on running app, gives me error. please me solve this.
Comments
Post a Comment