3

Cannot add to fragment transaction due to the following error, how would you go about this?

add() in FragmentTransaction cannot be applied to:
Expected Parameters: Actual Arguments:
int R.id.mapWithOverlay  
android.support.v4.app.Fragment _mapFragment  (com.google.android.gms.maps.MapFragment)
String "map"

Code is as follows:

android.support.v4.app.FragmentTransaction fragTx = getSupportFragmentManager().beginTransaction();
if (fragTx != null) {
     _mapFragment = MapFragment.newInstance(mapOptions);
     fragTx.add(R.id.mapWithOverlay, _mapFragment, "map");
     fragTx.commit();
} else {
     Toast.makeText(this, "Could not display the map", Toast.LENGTH_SHORT).show();
}

Thanks in advance.

Apqu
  • 4,880
  • 8
  • 42
  • 68

2 Answers2

7

Are you using SupportMapFragment (from the Support package) and not MapFragment

The regular MapFragment not work with SupportFragmentManager.

See the difference?

private class MyMap1 extends SupportMapFragment {

}

private class MyMap2 extends MapFragment {

}
Kuffs
  • 35,581
  • 10
  • 79
  • 92
0

I changed to the following to get it to work, hopefully this will help others?

FragmentTransaction fragTx = getFragmentManager().beginTransaction();
if (fragTx != null) {
     _mapFragment = MapFragment.newInstance(mapOptions);
     fragTx.add(R.id.mapWithOverlay, _mapFragment, "map");
     fragTx.commit();
} else {
     Toast.makeText(this, "Could not display the map", Toast.LENGTH_SHORT).show();
}
Apqu
  • 4,880
  • 8
  • 42
  • 68
  • This will only work if you are not using the Support Package. You cannot target early version of Android with this method as getFragmentManager does not exist on versions earlier than Api 11 – Kuffs Dec 20 '13 at 14:11
  • Ah I see, I will have to look into the differences between the two. Thanks for your help :-) – Apqu Dec 20 '13 at 14:13
  • If you want to target early versions, you need to implement my answer and use SupportMapFragment – Kuffs Dec 20 '13 at 14:14
  • Ok, thank you, I will accept your answer but leave mine here too as may help others who want to target just the later versions. – Apqu Dec 20 '13 at 14:23
  • Both answers could be correct based on what version of Android is being targeted. I assumed you wanted to use the support package as your project already has it referenced. As a side note, you may want to choose object names that do not clash with class names. i.e MapFragment. It could get confusing and you could accidentally import the wrong namespace. – Kuffs Dec 20 '13 at 14:26