第一次尝试开发Android应用。我想做的事情似乎很简单。我想要一个使用NavigationView的菜单。当点击其中一个项目时,一个新的片段是“#34; load"在主要布局上。
我成功创建了NavigationView,并且在单击项目菜单时可以显示一个片段。但如果我想点击另一个项目,应用程序就会停止。
这是我的(简化)代码:
包含主要布局的xml文件:
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Chabada"/>
当我想要显示特定片段时,我使用ID R.id.flContent。
对于我的每个片段,我都有类似的Java类:
public class ProfilFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.profil_fragment, container, false);
}
}
布局(xml文件):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Catégories"
android:id="@+id/categories"
android:layout_gravity="center" />
</LinearLayout>
这是我的主要活动java类的开头:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ProfilFragment categorie = new ProfilFragment();
// And the other fragments
/** more code, functions... **/
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
fragmentTransaction.replace(R.id.flContent,profil);
fragmentTransaction.commit();
} else if (id == R.id.nav_gallery) {
fragmentTransaction.replace(R.id.flContent,liste);
fragmentTransaction.commit();
}
// simplified here again, but it's the same code for every of my fragments
我错过了什么?
我使用了来自android developer guide及其doc的数据来创建此数据。我尝试在SO上咨询answered questions,但似乎与我的问题不符。
编辑1:
错误:
FATAL EXCEPTION: main
Process: com.example.arhyaa.youcook, PID: 32577
java.lang.IllegalStateException: commit already called
at android.app.BackStackRecord.commitInternal(BackStackRecord.java:713)
at android.app.BackStackRecord.commit(BackStackRecord.java:704)
at com.example.arhyaa.youcook.MainActivity.onNavigationItemSelected(MainActivity.java:103)
at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:151)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:318)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19761)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
答案 0 :(得分:3)
您的代码并未显示整个内容,但从错误中我可以猜测您使用相同的事务来提交您的更改两次
此行应位于onNavigationItemSelected方法
中FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
意味着每次要更改片段时都需要创建一个新事务,因为您之前已经提交了旧事件