我正在使用Android
撰写Navigation Drawer
个应用。我想在点击按钮时更改当前fragment
。
public void btnClick(){
...
changeFragment();
...
}
private void changeFragment() {
Fragment home = new Home();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.navigation_drawer, home);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
我在LogCat
中收到以下错误:
FATAL EXCEPTION: main
java.lang.UnsupportedOpetaionException: addView(View) is not supported in AdapterView
我的Home
片段已加载并显示在开头,移动到另一个片段并单击按钮后,我想自动返回Home
片段。
我怎样设法做到这一点?
编辑:完整错误日志
06-28 21:19:26.262: E/AndroidRuntime(5023): FATAL EXCEPTION: main
06-28 21:19:26.262: E/AndroidRuntime(5023): Process: com.susu.appclient, PID: 5023
06-28 21:19:26.262: E/AndroidRuntime(5023): java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.widget.AdapterView.addView(AdapterView.java:461)
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:968)
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.os.Handler.handleCallback(Handler.java:739)
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.os.Handler.dispatchMessage(Handler.java:95)
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.os.Looper.loop(Looper.java:135)
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.app.ActivityThread.main(ActivityThread.java:5254)
06-28 21:19:26.262: E/AndroidRuntime(5023): at java.lang.reflect.Method.invoke(Native Method)
06-28 21:19:26.262: E/AndroidRuntime(5023): at java.lang.reflect.Method.invoke(Method.java:372)
06-28 21:19:26.262: E/AndroidRuntime(5023): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-28 21:19:26.262: E/AndroidRuntime(5023): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:1)
如果我理解,您正在尝试更改导航抽屉下方的片段,而不是导航抽屉本身(这更复杂)。
在这一行:
ft.replace(R.id.navigation_drawer, home);
您必须替换应存在于activitylayout.xml上的FragmentLayout的R.id.navigation_drawer。
类似的东西:
ft.replace(R.id.fragment_layout, home);