java - Android Fragment Manager 的提交导致应用程序关闭

标签 java android android-fragments

第一次尝试开发Android应用程序。我想做的事情看起来很简单。我想要一个使用 NavigationView 的菜单。单击其中一项时,主布局上将“加载”一个新 fragment 。

我成功创建了 NavigationView,并且可以在单击项目菜单时显示一个 fragment 。但如果我想点击另一个项目,应用程序就会停止。

这是我的(简化的)代码:

具有主要布局的 xml 文件:

 <FrameLayout
    android:id="@+id/flContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Chabada"/>

当我想要显示特定 fragment 时,我使用 ID R.id.flContent。

对于我的每个 fragment ,我都有一个这样的 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>

这是我的主要 Activity 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创建这个。我尝试咨询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)

最佳答案

您的代码没有显示全部内容,但从错误中我可以猜测您正在使用同一事务来提交更改两次

此行应该位于您的 onNavigationItemSelected 方法内

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

这意味着您每次想要更改 fragment 时都需要创建一个新事务,因为您之前已经提交了旧事务

关于java - Android Fragment Manager 的提交导致应用程序关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36159956/

相关文章:

java - 为什么要在释放操作中获取锁

android - onClick 内部 fragment 调用 Activity

android - 在 WebView 中更改 URL 而不是打开浏览器

java - 重新启用 Eclipse 插件进度监视器中禁用的取消按钮?

java - Spring MVC 跨域验证 : Code Smell?

android - 使用上下文菜单在自定义 ListView 中突出显示所选项目?

android - 语义 - Android EditText 类

java - 序列化抽象类的问题

java - 无法从 fragment 按钮启动 Activity

java - 将映射键属性从 servlet 传递到 jsp