FragmentManager.beginTransaction() cannot be applied to (int, android.app.fragment)
I’m trying to create drawer navigation in Android and I’m running into some issues. I have a method in my MainActivity .java that handles my clicks on the drawer navigation and directs the user to the correct activity. Here’s how:
@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 penultimate line of .replace(R.id.container, objFragment): FragmentTransaction
cannot be applied to (int, android application fragment
).
Solution
If you use
SupportFragmentManager, you need to use Support Fragments
http://developer.android.com/reference/android/support/v4/app/Fragment.html
http://developer.android.com/reference/android/support/v4/app/FragmentManager.html
Contrast
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;