Trying to add a fragment to my fragment container FrameLayout

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:

  • Check if ScrollView is higher than screen / scrollable
  • -bash: android: command not found on Mac OSX
  • BitmapFactory.decodeStream returns null without exception
  • Asynctask vs Thread vs Services vs Loader
  • GetFragmentManager.findFragmentByTag() returns null
  • Why I can't purchase my own application on the Android market?
  • 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.

    Related posts:

    Error:Failed to open zip file. Gradle&#039;s dependency cache may be corrupt
    Alternative for AudioManager.setRingerMode() in Android 5.0 (API 21)
    Kotlin Coroutines the right way in Android
    Remove space between stacked TextViews
    How to determine if Android Application is started with JUnit testing instrumentation?
    Communication between PhoneGap, usb external device and Android host mode
  • IllegalStateException with PagerAdapter
  • Handling registration ID changes in Google Cloud Messaging on Android
  • Unable to mux both audio and video
  • what is difference between rtmp and rtsp protocol?
  • Save object while orientation change
  • Restore android app stack from background
  • 3 Solutions collect form web for “Trying to add a fragment to my fragment container FrameLayout”

    You forgot to commit() your transaction.

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

    add commit() like this

     getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFrag).commit();
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.