我创建了所有的片段和布局,我以前在上次创建应用时使用了eclipes中的switch语句,但现在它提示我使用if(id == R.id.nav_profile){ } else if(id == R.id.MakeUp)
有人能告诉我在导航栏中选择一个选项并打开一个新片段的正确代码吗?
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_profile) {
} else if (id == R.id.MakeUp) {
} else if (id == R.id.nav_Hairdressers) {
} else if (id == R.id.nav_Beauticians) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<group android:checkableBehavior="single">
<item android:id="@+id/nav_profile" android:icon="@android:drawable/ic_menu_camera"
android:title="Profile" />
<item android:id="@+id/MakeUp" android:icon="@android:drawable/ic_menu_gallery"
android:title="Make-Up" />
<item android:id="@+id/nav_Hairdressers" android:icon="@android:drawable/ic_menu_slideshow"
android:title="Hairdressers" />
<item android:id="@+id/nav_Beauticians" android:icon="@android:drawable/ic_menu_manage"
android:title="Beauticians" />
</group>
<item android:title="Communicate">
<menu>
<item android:id="@+id/nav_share" android:icon="@android:drawable/ic_menu_share"
android:title="Share" />
<item android:id="@+id/nav_send" android:icon="@android:drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
10-22 02:20:09.776 12351-12351/com.example.james.nixr E/FragmentManager: No view found for id 0x7f0c0076 (com.example.james.nixr:id/makup_layout) for fragment makup{28f4e84 #0 id=0x7f0c0076}
10-22 02:20:09.776 12351-12351/com.example.james.nixr E/FragmentManager: Activity state:
10-22 02:20:09.776 12351-12351/com.example.james.nixr E/FragmentManager: Local FragmentActivity c3caaf7 State:
10-22 02:20:09.776 12351-12351/com.example.james.nixr E/FragmentManager: mCreated=truemResumed=true mStopped=false mReallyStopped=false
10-22 02:20:09.776 12351-12351 / com.example.james.nixr E / FragmentManager:mLoadersStarted = true
块引用
10-22 02:20:09.776 12351-12351/com.example.james.nixr E/FragmentManager: FragmentManager misc state:
10-22 02:20:09.776 12351-12351/com.example.james.nixr E/FragmentManager: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@3f08d66d
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@3f08d66d
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: mCurState=5 mStateSaved=false mDestroyed=false
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: View Hierarchy:
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: com.android.internal.policy.impl.PhoneWindow$DecorView{3f15c794 V.E..... ... 0,0-1080,1920}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.widget.LinearLayout{30383fe7 V.E..... ... 0,0-1080,1920}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.view.ViewStub{d66b2a2 G.E..... ... 0,0-0,0 #1020436}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.widget.FrameLayout{2f8d423d V.E..... ... 0,0-1080,1920}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v7.internal.widget.FitWindowsLinearLayout{181fa32 V.E..... ... 0,0-1080,1920 #7f0c0057 app:id/action_bar_root}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v7.internal.widget.ViewStubCompat{a739b33 G.E..... ... 0,0-0,0 #7f0c0058 app:id/action_mode_bar_stub}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager:
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v7.internal.widget.ContentFrameLayout{333ef383 V.E..... ... 0,0- 1080,1920 #1020002 android:id/content}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v4.widget.DrawerLayout{1a51c900 VFED.... ... 0,0-1080,1920 #7f0c006b app:id/drawer_layout}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.support.design.widget.CoordinatorLayout{1610c539 V.ED.... ... 0,0- 1080,1920}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.support.design.widget.AppBarLayout{451877e V.E..... ... 0,75-1080,243}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v7.widget.Toolbar{377344df V.E..... ... 0,0-1080,168 #7f0c006d app:id/toolbar}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.widget.TextView{3caf7bf5 V.ED.... ... 180,43-297,124}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.widget.ImageButton{1e8a752c VFED..C. ... 0,0-168,168}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v7.widget.ActionMenuView{365628f2 V.E..... ... 972,0-1080,168}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v7.widget.ActionMenuPresenter$OverflowMenuButton{555b43 VFED..C. ... 0,12-108,156}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager:
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.widget.RelativeLayout{22fa9d8a V.E..... ... 0,243-1080,1920}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v7.widget.AppCompatTextView{23714ffb V.ED.... ... 48,48-273,105}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.support.design.widget.FloatingActionButton{34bfb818 VFED..C. ... 864,1704-1032,1872 #7f0c006e app:id/fab}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.support.design.widget.NavigationView{1a48e271 V.E..... ... 0,0-888,1920 #7f0c006c app:id/nav_view}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.support.design.internal.NavigationMenuView{3a6d0856 VFED.V.. F.. 0,0- 888,1920 #7f0c0072 app:id/design_navigation_view}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.widget.LinearLayout{14043dc4 V.E..... ... 0,0-888,504 #7f0c0071 app:id/navigation_header_container}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.widget.LinearLayout{1a3834ad V.E..... ... 0,0-888,480}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v7.widget.AppCompatImageView{34c953e2 V.ED.... ... 48,78-192,270 #7f0c0078 app:id/imageView}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v7.widget.AppCompatTextView{1413c373 V.ED.... ... 48,270-840,375}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v7.widget.AppCompatTextView{2fe97230 V.ED.... ... 48,375-592,432 #7f0c0079 app:id/textView}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.support.design.internal.NavigationMenuItemView{29327992 V.ED..C. ... 0,504-888,648}
10-22 02:20:09.786 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{4746f63 V.ED.... ... 48,0-840,144 #7f0c0073 app:id/design_menu_item_text}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.widget.FrameLayout{2915c760 V.E..... ... 840,0-840,144 #7f0c0075 app:id/design_menu_item_action_area}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.support.design.internal.NavigationMenuItemView{327a9cde V.ED..C. ..P 0,648-888,792}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{bb49ebf V.ED.... ..P 48,0- 840,144 #7f0c0073 app:id/design_menu_item_text}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.widget.FrameLayout{afff98c V.E..... ..P 840,0-840,144 #7f0c0075 app:id/design_menu_item_action_area}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.support.design.internal.NavigationMenuItemView{158fa8ea V.ED..C. ... 0,792-888,936}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{29e67db V.ED.... ... 48,0-840,144 #7f0c0073 app:id/design_menu_item_text}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.widget.FrameLayout{f792278 V.E..... ... 840,0-840,144 #7f0c0075 app:id/design_menu_item_action_area}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.support.design.internal.NavigationMenuItemView{325c209a V.ED..C. ... 0,936-888,1080}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{1444fdcb V.ED.... ... 48,0-840,144 #7f0c0073 app:id/design_menu_item_text}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.widget.FrameLayout{3cb619a8 V.E..... ... 840,0-840,144 #7f0c0075 app:id/design_menu_item_action_area}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.support.design.internal.NavigationMenuItemView{124bac66 V.ED..C. ... 0,1080-888,1224}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{15a3aba7 V.ED.... ... 48,0-840,144 #7f0c0073 app:id/design_menu_item_text}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager:
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.widget.FrameLayout{3f272854 V.E..... ... 840,0-840,144 #7f0c0075 app:id/design_menu_item_action_area}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.widget.FrameLayout{24b18d8d V.E..... ... 0,1224-888,1251}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.view.View{c86b42 V.ED.... ... 0,24-888,27}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v7.widget.AppCompatTextView{2b67589 V.ED.... ... 0,1251-888,1395}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.support.design.internal.NavigationMenuItemView{3dbca5b5 V.ED..C. ... 0,1395-888,1539}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{22cde44a V.ED.... ... 48,0-840,144 #7f0c0073 app:id/design_menu_item_text}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.widget.FrameLayout{2874efbb V.E..... ... 840,0-840,144 #7f0c0075 app:id/design_menu_item_action_area}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.support.design.internal.NavigationMenuItemView{2e5ee831 V.ED..C. ... 0,1539-888,1683}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager:
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{300dfb16 V.ED.... ... 48,0-840,144 #7f0c0073 app:id/design_menu_item_text}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/FragmentManager: android.widget.FrameLayout{178cc97 V.E..... ... 840,0-840,144 #7f0c0075 app:id/design_menu_item_action_area}
10-22 02:20:09.796 12351-12351/com.example.james.nixr D/AndroidRuntime: Shutting down VM
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/AndroidRuntime: FATAL EXCEPTION: main
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/AndroidRuntime: Process: com.example.james.nixr, PID: 12351
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/AndroidRuntime: java.lang.IllegalArgumentException: No view found for id 0x7f0c0076 (com.example.james.nixr:id/makup_layout) for fragment makup{28f4e84 #0 id=0x7f0c0076}
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:896)
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/AndroidRuntime: at android.app.BackStackRecord.run(BackStackRecord.java:833)
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/AndroidRuntime: at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/AndroidRuntime: at android.app.FragmentManagerImpl$1.run(FragmentManager.java:452)
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5832)
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
10-22 02:20:09.796 12351-12351/com.example.james.nixr E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
10-22 02:20:11.116 12351-12351/com.example.james.nixr I/Process: Sending signal. PID: 12351 SIG: 9
答案 0 :(得分:0)
Switch应该运行得很好,关于使用Fragments和FragmentManager的问题是什么?这是使用NavigationView的一个很好的教程: http://www.android4devs.com/2015/06/navigation-view-material-design-support.html
答案 1 :(得分:0)
好的,从您在此问题上留下的各种评论来判断,我猜测您的实际问题是,当您拨打{{1}时,如何向视图中添加新的片段}。你会做这样的事情......
onNavigationItemSelected()
或者,如果一个片段已经显示,您需要替换它......
FragmentManager fragmentManager = getFragmentManager(); //or getSupportFragmentManager() if you're using support library
FragmentTransaction transaction = fragmentManager.beginTransaction();
MakeupFragment makeupFragment = MakeupFragment.newInstance();
transaction.add(R.id.container_that_holds_fragment, makeupFragment);
transaction.addToBackStack(null);
transaction.commit();
这是您要查找的信息吗?
编辑:回应以下评论......
如果您收到错误HairdresserFragment hairdresserFragment = HairdresserFragment.newInstance();
transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.container_that_holds_fragment, hairdresserFragment);
transaction.addToBackStack(null);
transaction.commit();
或类似内容,则表示您尚未在片段中定义此方法。确保您的片段定义了以下方法...
cannot resolve method newInstance()
你的片段应该使用这个静态方法实例化自己,然后将创建的片段传回给任何调用它的人。在片段中实施此方法后,只需拨打public static YourFragment newInstance() {
YourFragment fragment = new YourFragment();
return fragment;
}