java - Adding tab Navigation to my Fragment -
i new android developing . i've made tab navigation activity, transfer code fragment project, encountered errors . can me proper way inside fragment .
note : used import android.support.v4.app.fragment;
on both frmslide , fragmentfeedfragment
my frmslide code this
public class frmslide extends appcompatactivity implements navigationdrawerfragment.navigationdrawercallbacks { /** * fragment managing behaviors, interactions , presentation of navigation drawer. */ private navigationdrawerfragment mnavigationdrawerfragment; /** * used store last screen title. use in {@link #restoreactionbar()}. */ private charsequence mtitle; viewpager viewpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_frm_slide); mnavigationdrawerfragment = (navigationdrawerfragment) getsupportfragmentmanager().findfragmentbyid(r.id.navigation_drawer); mtitle = gettitle(); // set drawer. mnavigationdrawerfragment.setup( r.id.navigation_drawer, (drawerlayout) findviewbyid(r.id.drawer_layout)); } @override public void onnavigationdraweritemselected(int position) { fragmentmanager fragmentmanager = getsupportfragmentmanager(); // update main content replacing fragments fragmentmanager.begintransaction().replace(r.id.container, placeholderfragment.newinstance(position + 1)).commit(); fragment fragment; switch (position + 1) { case 1: //profile fragment = new fragmentprofilefragment(); fragmentmanager.begintransaction().replace(r.id.container, fragment).commit(); break; case 2: //startactivity(new intent(this, fragmentfeed.class)); //feed fragment = new fragmentfeedfragment(); fragmentmanager.begintransaction().replace(r.id.container, fragment).commit(); break; case 3: //settings fragment = new fragmentsettingsfragment(); fragmentmanager.begintransaction().replace(r.id.container, fragment).commit(); break; case 4: //about fragment = new fragmentaboutfragment(); fragmentmanager.begintransaction().replace(r.id.container, fragment).commit(); break; } log.d("myslide", "onnavigation " + position); } //selected/clicked menu on slide menu public void onsectionattached(int number) { switch (number) { case 1: mtitle = getstring(r.string.title_section1); //toast.maketext(frmslide.this, "profile selected", toast.length_short).show(); break; case 2: mtitle = getstring(r.string.title_section2); //toast.maketext(frmslide.this, "feed selected", toast.length_short).show(); break; case 3: mtitle = getstring(r.string.title_section3); //toast.maketext(frmslide.this, "settings selected", toast.length_short).show(); break; case 4: mtitle = getstring(r.string.title_section4); //toast.maketext(frmslide.this, "about selected", toast.length_short).show(); break; case 5: alertdialog.builder builder = new alertdialog.builder(this); builder.setmessage("are sure want logout?") .setcancelable(false) .setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { finish(); startactivity(new intent(frmslide.this, mainactivity.class));//close activity example: myactivity.java } }) .setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { // code if want dialog.cancel(); restoreactionbar(); } }); alertdialog alert = builder.create(); alert.show(); break; } } public void restoreactionbar() { actionbar actionbar = getsupportactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_standard); actionbar.setdisplayshowtitleenabled(true); actionbar.settitle(mtitle); } @override public boolean oncreateoptionsmenu(menu menu) { if (!mnavigationdrawerfragment.isdraweropen()) { // show items in action bar relevant screen // if drawer not showing. otherwise, let drawer // decide show in action bar. getmenuinflater().inflate(r.menu.frm_slide, menu); restoreactionbar(); return true; } return super.oncreateoptionsmenu(menu); } @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; } return super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { /** * fragment argument representing section number * fragment. */ private static final string arg_section_number = "section_number"; /** * returns new instance of fragment given section * number. */ public static placeholderfragment newinstance(int sectionnumber) { placeholderfragment fragment = new placeholderfragment(); bundle args = new bundle(); args.putint(arg_section_number, sectionnumber); fragment.setarguments(args); return fragment; } public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_frm_slide, container, false); return rootview; } @override public void onattach(activity activity) { super.onattach(activity); ((frmslide) activity).onsectionattached( getarguments().getint(arg_section_number)); } }
}
here in fragmentfeedfragment .
public class fragmentfeedfragment extends fragment implements actionbar.tablistener { viewpager viewpager; actionbar actionbar; public fragmentfeedfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.activity_fragment_feed, container, false); } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); viewpager = (viewpager) getview().findviewbyid(r.id.pager); viewpager.setadapter(new myfeedadapter(getfragmentmanager())); viewpager.setonpagechangelistener(new viewpager.onpagechangelistener() { @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { log.d("vivz", "ontabselected @ " + "position " + position + "from " + positionoffset + "pixels # " + positionoffsetpixels); } @override public void onpageselected(int position) { actionbar.setselectednavigationitem(position); //log.d("vivz", "ontabselected @ " + "position " + position); } @override public void onpagescrollstatechanged(int state) { } }); actionbar = ((actionbaractivity)getactivity()).getsupportactionbar(); //actionbar = getsupportactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); actionbar.tab tab1 = actionbar.newtab(); tab1.settext("joy"); tab1.settablistener(this); actionbar.tab tab2 = actionbar.newtab(); tab2.settext("sadness"); tab2.settablistener(this); actionbar.tab tab3 = actionbar.newtab(); tab3.settext("anger"); tab3.settablistener(this); actionbar.addtab(tab1); actionbar.addtab(tab2); actionbar.addtab(tab3); } @override public void ontabselected(actionbar.tab tab, fragmenttransaction ft) { viewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(actionbar.tab tab, fragmenttransaction ft) { } @override public void ontabreselected(actionbar.tab tab, fragmenttransaction ft) { }
}
class myfeedadapter extends fragmentpageradapter {
public myfeedadapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { fragment fragment = null; if (position==0){ fragment = new fragmentjoyfragment(); } if (position==1){ fragment = new fragmentsadnessfragment(); } if (position==2){ fragment = new fragmentangerfragment(); } return fragment; } @override public int getcount() { return 3; }
}
and here error showed up
10-15 11:11:37.781 16034-16034/com.example.agnes.myapplication e/androidruntime﹕ fatal exception: main java.lang.classcastexception: com.example.agnes.myapplication.frmslide cannot cast android.support.v7.app.actionbaractivity @ com.example.agnes.myapplication.fragmentfeedfragment.onactivitycreated(fragmentfeedfragment.java:54) @ android.support.v4.app.fragment.performactivitycreated(fragment.java:1970) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1051) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1207) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:738) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1572) @ android.support.v4.app.fragmentmanagerimpl$1.run(fragmentmanager.java:493) @ android.os.handler.handlecallback(handler.java:615) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:213) @ android.app.activitythread.main(activitythread.java:4787) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:789) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:556) @ dalvik.system.nativestart.main(native method)
in fragmentfeedfragment.java:54,
change
actionbar = ((actionbaractivity)getactivity()).getsupportactionbar();
to
actionbar = getactionbar();
Comments
Post a Comment