android - App crashes when a opening a new item ( fragment) from the navigationDrawer (see code)? -
i trying make app navigationview . navigation view has many options when clicked opens new fragment.i have set home fragment default. app crashing whenever click option navigationview. new android development, please help!
my log
ava.lang.illegalargumentexception: no view found id 0x7f0c0086 (rishabh.example.com.navigationdrawer:id/home_id) fragment profilefragment{10c46e27 #1 id=0x7f0c0086} @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1059) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1252) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:742) @ 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:135) @ android.app.activitythread.main(activitythread.java:5343) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:905) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:700)
my mainactivity.java
import android.os.bundle; import android.os.persistablebundle; import android.support.design.widget.navigationview; import android.support.v4.widget.drawerlayout; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.util.log; import android.view.menuitem; public class mainactivity extends appcompatactivity { drawerlayout drawerlayout; android.support.v7.app.actionbardrawertoggle actionbardrawertoggle; toolbar toolbar; android.support.v4.app.fragmenttransaction fragmenttransaction; navigationview navigationview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar=(toolbar)findviewbyid(r.id.toolbar_layout); setsupportactionbar(toolbar); drawerlayout=(drawerlayout)findviewbyid(r.id.drawerlayout); actionbardrawertoggle=new android.support.v7.app.actionbardrawertoggle(this,drawerlayout,toolbar,r.string.drawer_open,r.string.drawer_close); drawerlayout.adddrawerlistener(actionbardrawertoggle); fragmenttransaction=getsupportfragmentmanager().begintransaction(); fragmenttransaction.add(r.id.container_fragment,new homefragment()); fragmenttransaction.commit(); getsupportactionbar().settitle("home"); navigationview=(navigationview)findviewbyid(r.id.navview); navigationview.setnavigationitemselectedlistener(new navigationview.onnavigationitemselectedlistener() { @override public boolean onnavigationitemselected(menuitem item) { switch(item.getitemid()){ case r.id.home_id: fragmenttransaction=getsupportfragmentmanager().begintransaction(); fragmenttransaction.replace(r.id.home_id,new homefragment()); fragmenttransaction.commit(); getsupportactionbar().settitle("home"); item.setchecked(true); drawerlayout.closedrawers(); break; case r.id.profile_id: log.i("tag","inside profile"); fragmenttransaction=getsupportfragmentmanager().begintransaction(); fragmenttransaction.replace(r.id.home_id,new profilefragment()); fragmenttransaction.commit(); log.i("tag","inside profile commit"); getsupportactionbar().settitle("profile"); item.setchecked(true); drawerlayout.closedrawers(); break; case r.id.tracker_id: fragmenttransaction=getsupportfragmentmanager().begintransaction(); fragmenttransaction.replace(r.id.home_id,new tracerfragment()); fragmenttransaction.commit(); getsupportactionbar().settitle("tracker"); item.setchecked(true); drawerlayout.closedrawers(); break; case r.id.siren_id: fragmenttransaction=getsupportfragmentmanager().begintransaction(); fragmenttransaction.replace(r.id.home_id,new sirenfragment()); fragmenttransaction.commit(); getsupportactionbar().settitle("siren"); item.setchecked(true); drawerlayout.closedrawers(); break; case r.id.settings_id: fragmenttransaction=getsupportfragmentmanager().begintransaction(); fragmenttransaction.replace(r.id.home_id,new settingsfragment()); fragmenttransaction.commit(); getsupportactionbar().settitle("settings"); item.setchecked(true); drawerlayout.closedrawers(); break; case r.id.support_id: fragmenttransaction=getsupportfragmentmanager().begintransaction(); fragmenttransaction.replace(r.id.home_id,new supportfragment()); fragmenttransaction.commit(); getsupportactionbar().settitle("support"); item.setchecked(true); drawerlayout.closedrawers(); break; case r.id.about_id: fragmenttransaction=getsupportfragmentmanager().begintransaction(); fragmenttransaction.replace(r.id.home_id,new aboutusfragment()); fragmenttransaction.commit(); getsupportactionbar().settitle("about us"); item.setchecked(true); drawerlayout.closedrawers(); break; } return true; } }); }
activity_layout
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/drawerlayout" tools:context="rishabh.example.com.navigationdrawer.mainactivity"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include android:layout_height="wrap_content" android:layout_width="match_parent" layout="@layout/toolbar_layout" /> <framelayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/container_fragment"> </framelayout> </linearlayout> <android.support.design.widget.navigationview android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/navview" android:layout_gravity="start" app:menu="@menu/drawer_menu" app:headerlayout="@layout/navigation_drawer_head" > </android.support.design.widget.navigationview>
profilefragment
public class profilefragment extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment return inflater.inflate(r.layout.fragment_profile, container, false); } }
there no home_id in layout. container_fragment should loading fragments.
fragmenttransaction.replace(r.id.home_id,new homefragment()); fragmenttransaction.commit();
replace with
fragmenttransaction.replace(r.id.container_fragment,new homefragment());
do same change replace() calls(container_fragment in place of home_id).
Comments
Post a Comment