Android add TabListener inside ActionBarActivity has stopped -
i'm building app mainactivity
extends actionbaractivity
, inplemented navigationdrawerfragment.navigationdrawercallbacks
, has actionbar want have tabs. after runing project error , alert me has stopped
:
caused by: java.lang.classcastexception: android.support.v4.widget.drawerlayout cannot cast android.support.v4.view.viewpager @ ir.tsms.mainactivity.oncreate(mainactivity.java:43)
i want have menuslide
inside tab
on mainactivity
code:
package ir.tsms; import android.app.activity; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.support.v4.app.fragmenttransaction; import android.support.v4.view.viewpager; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbar; import android.support.v7.app.actionbaractivity; import android.view.*; import java.util.locale; public class mainactivity extends actionbaractivity implements navigationdrawerfragment.navigationdrawercallbacks, actionbar.tablistener { private navigationdrawerfragment mnavigationdrawerfragment; private charsequence mtitle; sectionspageradapter msectionspageradapter; viewpager mviewpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mnavigationdrawerfragment = (navigationdrawerfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.navigation_drawer); mtitle = gettitle(); mnavigationdrawerfragment.setup(r.id.navigation_drawer, (drawerlayout) findviewbyid(r.id.pager)); final actionbar actionbar = getsupportactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); msectionspageradapter = new sectionspageradapter( getsupportfragmentmanager()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.pager); /*<-- line has error */ mviewpager.setadapter(msectionspageradapter); mviewpager .setonpagechangelistener(new viewpager.simpleonpagechangelistener() { @override public void onpageselected(int position) { actionbar.setselectednavigationitem(position); } }); // each of sections in app, add tab action bar. (int = 0; < msectionspageradapter.getcount(); i++) { actionbar.addtab(actionbar.newtab() .settext(msectionspageradapter.getpagetitle(i)) .settablistener(this)); } } @override public void onnavigationdraweritemselected(int position) { // update main content replacing fragments fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmentmanager .begintransaction() .replace(r.id.container, placeholderfragment.newinstance(position + 1)).commit(); } public void onsectionattached(int number) { switch (number) { case 1: mtitle = getstring(r.string.title_section1); break; case 2: mtitle = getstring(r.string.title_section2); break; case 3: mtitle = getstring(r.string.title_section3); 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.main, 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(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } @override public void ontabunselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { } @override public void ontabreselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { } @override public void ontabselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { } /** * 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_main, container, false); return rootview; } @override public void onattach(activity activity) { super.onattach(activity); ((mainactivity) activity).onsectionattached(getarguments().getint( arg_section_number)); } } public class sectionspageradapter extends fragmentpageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } public fragment getitem(int position) { // getitem called instantiate fragment given page. // return placeholderfragment (defined static inner class // below). return placeholderfragment.newinstance(position + 1); } public int getcount() { // show 3 total pages. return 3; } public charsequence getpagetitle(int position) { locale l = locale.getdefault(); switch (position) { case 0: return getstring(r.string.title_section1).touppercase(l); case 1: return getstring(r.string.title_section2).touppercase(l); case 2: return getstring(r.string.title_section3).touppercase(l); } return null; } } }
mainactivity
layout:
<!-- drawerlayout intended used top-level content view using match_parent both width , height consume full space available. --> <android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="ir.tsms.mainactivity" > <framelayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> <fragment android:id="@+id/navigation_drawer" android:name="ir.tsms.navigationdrawerfragment" android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent" android:layout_gravity="start" tools:layout="@layout/fragment_navigation_drawer" /> </android.support.v4.widget.drawerlayout>
how resolve problem?
your problem:
casting android.support.v4.widget.drawerlayout
viewpager
. in line finding navigationdrawer cast view pager, must change navigationdrawer or put view pager in layout , find that.
<!-- drawerlayout intended used top-level content view using match_parent both width , height consume full space available. --> <android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="ir.tsms.mainactivity" > <android.support.v4.view.viewpager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" </android.support.v4.view.viewpager> <fragment android:id="@+id/navigation_drawer" android:name="ir.tsms.navigationdrawerfragment" android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent" android:layout_gravity="start" tools:layout="@layout/fragment_navigation_drawer" /> </android.support.v4.widget.drawerlayout>
Comments
Post a Comment