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

I am trying to create a navigation box in Android and I am having a problem. I have a method in my MainActivity.java that handles my clicks in the navigation box and directs the user to the correct activity. Method 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 am getting an error from the third to the last line in .replace(R.id.container, objFragment)

:FragmentTransaction cannot be applied to (int, android.app.fragment)

+3


source to share


1 answer


You need to use support snippets if you are using SupportFragmentManager

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

against

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

You cannot 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;

from

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

+5


source







All Articles