android - Cannot replace fragments in an activity -
my activity contains navigationdrawer
.i need change fragments
in activity when items in navigation drawer pressed.i error while replacing fragments.
activity
package in.yusoft.yuride.yuride; import android.app.fragmentmanager; import android.app.fragmenttransaction; import android.content.context; import android.support.v4.app.fragment; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbar; import android.support.v7.app.actionbardrawertoggle; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.baseadapter; import android.widget.imageview; import android.widget.listview; import android.widget.textview; import android.widget.toast; public class homeactivity extends appcompatactivity implements adapterview.onitemclicklistener { drawerlayout drawer_layout; listview drawer_list; actionbardrawertoggle drawer_toggle; actionbar abar; myadapter myadapter; android.support.v4.app.fragmentmanager fm; android.support.v4.app.fragmenttransaction ft; fragment def_frag,about_us,cancel_ride,fares,profile,ride_history,ride_updates,success,your_places; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_home); def_frag=new success(); fm=getsupportfragmentmanager(); ft=fm.begintransaction(); ft.replace(r.id.home_fragment, new cancelride()); ft.commitallowingstateloss();; myadapter=new myadapter(this); drawer_layout = (drawerlayout) findviewbyid(r.id.drawer_layout); drawer_layout.adddrawerlistener(drawer_toggle); drawer_list = (listview) findviewbyid(r.id.drawer_list); drawer_list.setadapter(myadapter); //swtting drawer width int width = getresources().getdisplaymetrics().widthpixels/2; drawerlayout.layoutparams params = (android.support.v4.widget.drawerlayout.layoutparams) drawer_list.getlayoutparams(); params.width = width; drawer_list.setlayoutparams(params); drawer_list.setonitemclicklistener(this); abar = getsupportactionbar(); drawer_toggle = new actionbardrawertoggle(this, drawer_layout, r.string.app_name, r.string.app_name) { //** called when drawer has settled in closed state. *//* public void ondrawerclosed(view view) { super.ondrawerclosed(view); getactionbar().settitle("drawer opened"); invalidateoptionsmenu(); } //** called when drawer has settled in open state. *//* public void ondraweropened(view drawerview) { super.ondraweropened(drawerview); getactionbar().settitle("drawer closed"); invalidateoptionsmenu(); // creates call onprepareoptionsmenu() } }; drawer_toggle.syncstate(); if (abar != null) { abar.sethomebuttonenabled(true); abar.setdisplayhomeasupenabled(true); } } @override public boolean onoptionsitemselected(android.view.menuitem item) { return drawer_toggle.onoptionsitemselected(item); } @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { object o = drawer_list.getitematposition(position); toast.maketext(homeactivity.this, ""+o.tostring(), toast.length_short).show(); ft=fm.begintransaction(); switch (position) { case 0: ft.replace(r.id.home_fragment,new profile()).addtobackstack(null).commit(); break; case 1: ft.replace(r.id.home_fragment,new ridehistory()).addtobackstack(null).commit(); break; case 3: ft.replace(r.id.home_fragment,new fares()).addtobackstack(null).commit(); break; case 4: ft.replace(r.id.home_fragment,new yourplaces()).addtobackstack(null).commit(); break; case 6: ft.replace(r.id.home_fragment,new cancelride()).addtobackstack(null).commit(); break; case 7: ft.replace(r.id.home_fragment,new rideupdates()).addtobackstack(null).commit(); break; case 8: ft.replace(r.id.home_fragment,new aboutus()).addtobackstack(null).commit(); break; } } class myadapter extends baseadapter { context context; string[] drawer_items; public myadapter(context context) { this.context=context; drawer_items=context.getresources().getstringarray(r.array.drawer_items); } @override public int getcount() { return drawer_items.length; } @override public object getitem(int position) { return drawer_items[position]; } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { view row=null; if(convertview==null) { layoutinflater inflater= (layoutinflater) context.getsystemservice(context.layout_inflater_service); row=inflater.inflate(r.layout.drawer_row,parent,false); } else { row=convertview; } imageview drawer_icon= (imageview) row.findviewbyid(r.id.drawer_icon); textview drawer_text= (textview) row.findviewbyid(r.id.drawer_text); drawer_text.settext(drawer_items[position]); drawer_icon.setimageresource(r.drawable.tick); return row; } } }
xml
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/drawer_layout"> <framelayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/home_fragment"> </framelayout> <listview android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="left" android:id="@+id/drawer_list" android:background="@color/blue_normal"> </listview> </android.support.v4.widget.drawerlayout>
i followng error while doing this
fatal exception: main process: in.yusoft.yuride.yuride, pid: 2813 java.lang.runtimeexception: in.yusoft.yuride.yuride.homeactivity@f1cb3b3 must implement onfragmentinteractionlistener @ in.yusoft.yuride.yuride.profile.onattach(profile.java:83) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1019) @ android.support.v4.app.backstackrecord.ensurefragmentsareinitialized(backstackrecord.java:1081) @ android.support.v4.app.backstackrecord.begintransition(backstackrecord.java:1036) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:662) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1617) @ android.support.v4.app.fragmentmanagerimpl$1.run(fragmentmanager.java:517) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:6066) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:770) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:660)
you can implement fragmentdrawer.fragmentdrawerlistener
resolve produced error....and can this...
//when navigation item selected user @override public void ondraweritemselected(view view, int position) { displayview(position); } public void displayview(int position) { string title = getstring(r.string.app_name); switch (position) { case 0: fragment = new fragment1(); break; case 1: fragment = new fragment2(); break; case 2: fragment = new fragment3(); break; case 3: //and on.... default: break; } if (fragment != null) { fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.replace(r.id.container_body, fragment).addtobackstack("fragback"); fragmenttransaction.commit(); } }
Comments
Post a Comment