26

I have created an xml file called editor.xml which contains a FrameLayout. In my main activity I am trying to add my custom fragment to my FrameLayout.

The error I receive when trying to add my fragment is:

The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, editorFrag)

However my editorFrag extends Fragment so I am confused on why this is happening. Below is my code for the files I have mentioned. Any help is appreciated.

Editor.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

editorFrag.java

public class editorFrag extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) 
    {

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.newlevel, container, false);
    }
}

MainActivity.java

public class editorActivity extends FragmentActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.editor);

        // Check that the activity is using the layout version with the fragment_container FrameLayout
        if(findViewById(R.id.fragment_container) != null)
        {
            // if we are being restored from a previous state, then we dont need to do anything and should
            // return or else we could end up with overlapping fragments.
            if(savedInstanceState != null)
                return;

            // Create an instance of editorFrag
            editorFrag firstFrag = new editorFrag();

            // add fragment to the fragment container layout
            getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFrag);
        }
    } 
}

Answered:

Luksprog answered this problem for me below by telling me to check my imports. Eclipse chose to import the SDK version of Fragment instead of the support version that I needed. Thank you for the help.

Pedrom
  • 378
  • 1
  • 5
  • 13
  • Check your imports. See if you didn't imported the `SDK` version of `Fragment` instead of the compatibility package `Fragment`. – user Aug 24 '12 at 17:27
  • You are exactly right, I let eclipse import for me and it did indeed choose the SDK version where in my activity it chose the support version. Thank you very much – Pedrom Aug 24 '12 at 17:32
  • @Perdom mind accepting the answer for this question? Thanks. http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Marcin Orlowski Nov 30 '16 at 17:52

4 Answers4

33

You forgot to commit() your transaction.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Your right in that fact but I took the commit off to try and narrow down my issue. Thank you for your answer though, I will need to add the commit() again. – Pedrom Aug 24 '12 at 17:34
5

You also forgot to call the addtoBackStack() method, otherwise your app closes when you hit the back button.

Code Maverick
  • 20,171
  • 12
  • 62
  • 114
seken1
  • 51
  • 1
  • 1
5

add commit() like this

 getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFrag).commit();
Milon
  • 2,221
  • 23
  • 27
0
 1-   //Add fragment container in xml file
    
    <androidx.fragment.app.FragmentContainerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragment_container_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    </androidx.fragment.app.FragmentContainerView>


2-  //Implementation of BackStack

        fragmentTransaction.setReorderingAllowed(true);
        fragmentTransaction.addToBackStack("name");