rakeshdas
rakeshdas

Reputation: 2533

FragmentManager.beginTransaction() cannot be applied to (int, android.app.fragment)

I am trying to create a navigation drawer in Android and I have run into a bit of a problem. I have a method in my MainActivity.java that handles my clicks on the navigation drawer and directs the user to the proper activity. The method as shown here:

      @Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    android.app.Fragment objFragment = null;
    switch (position){
        case 0:
            objFragment= new menu1_Fragment();
            break;
        case 1:
            objFragment = new menu2_Fragment();
            break;
        case 2:
            objFragment = new menu3_Fragment();
            break;
    }
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, objFragment)
            .commit();
}

My problem is that I get an error on the third to last line in .replace(R.id.container, objFragment) : FragmentTransaction cannot be applied to (int, android.app.fragment)

Upvotes: 4

Views: 9754

Answers (1)

Blundell
Blundell

Reputation: 76458

You need to use support Fragments if you use the SupportFragmentManager

http://developer.android.com/reference/android/support/v4/app/Fragment.html http://developer.android.com/reference/android/support/v4/app/FragmentManager.html

vs

http://developer.android.com/reference/android/app/FragmentManager.html http://developer.android.com/reference/android/app/Fragment.html

You can never mix and match between the two

// update the main content by replacing fragments
    android.support.v4.app.Fragment objFragment = null;
    switch (position){
        case 0:

clarity, replace:

android.app.Fragment objFragment = null;

with

android.support.v4.app.Fragment objFragment = null;

Upvotes: 6

Related Questions