0

Trying to develop an Android app for the first time. What I'd like to do seems quite simple. I want a menu using NavigationView. When one of its items is clicked, a new fragment is "load" on the main layout.

I successfully created the NavigationView and I can make one fragment appear when on item's menu is clicked. But if I want to click on another item, the app stopped.

Here is my (simplified) code :

An xml file with the main layout :

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

I use the ID R.id.flContent when I want to make a specific fragment appear.

For every of my fragment, I have a Java class like that :

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);
      }
    }

And a layout (xml file) :

<?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>

Here is a the beginning of my main activity java class :

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

What did I miss ?

I used data from android developer guide and its doc to create this. I tried to consult answered questions on SO but it seems not to match with my problem.

Edit 1 :

The error :

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)
Community
  • 1
  • 1
Arhyaa
  • 369
  • 1
  • 3
  • 21

1 Answers1

3

your code doesn't show the whole thing, but from the error I can guess that you are using the same transaction to commit your changes twice

this line should be inside your onNavigationItemSelected method

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

meaning you need to create a new transaction each time you want to change the fragment because you already committed the old one before

Hassan Khallouf
  • 1,170
  • 1
  • 13
  • 30
  • Thanks a lot! I understood that it came from the commit() but I didn't imagine it was that. Garrus Vakarian, always here when he's needed :-) – Arhyaa Mar 22 '16 at 16:44