10

I have problems to access the fragment of the map. getFragmentManager().findFragmentById(R.id.map)) returns always null. I don't know why. What's the problem?

Thank you!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

        <TextView
            android:id="@+id/tvNombreCentro"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />


    <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="tag_fragment_map" />

    </LinearLayout>

And in the activity after the setContentView I try to access to the map, but I receive an exception

public class Mapa extends Activity {
private GoogleMap mMap;
private ActionBar ab;

private TextView tvNombreCentro;
private TextView tvTelefonoValor;
private TextView tvEMailValor;
private TextView tvWebValor;
private TextView tvDireccionValor;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.mapa_centro);
    GoogleMap mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
}
MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
user1852854
  • 199
  • 1
  • 1
  • 11

4 Answers4

21

use this way:

SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);

            map = mapFrag.getMap();

here is full code with sample:

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
  • 11
    As a note, if you use Android support library to display a map, please make sure that your layout xml file that contains the map fragment should have an android:name attribute value to `com.google.android.gms.maps.SupportMapFragment` rather than `com.google.android.gms.maps.MapFragment`, otherwise it won't work and keep returning `null` – Aryo Jul 09 '14 at 00:30
  • Excellent! it helps me!. – mr_ivan777 Dec 13 '15 at 18:15
2

In case your problem is not solved, try this :

As you are using SupportMapFragment, while retrieving map, use

   GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

Also, change

   public class Mapa extends Activity {

to

   public class Mapa extends FragmentActivity {
Divya Motiwala
  • 1,659
  • 1
  • 16
  • 24
  • sorry for the delay. The answer is correct but I don't need to change the extends, only getSupportFragmentManager. Thank you! – user1852854 Jul 08 '13 at 13:42
1

Change in the layout

android:name = "com.google.android.gms.maps.SupportMapFragment"

by

android:name = "com.google.android.gms.maps.MapFragment".

Or, you can use getSupportFragmentManager() instead of getFragmentManager().

1

I had a similar problem when trying to get MapFragment but getting null because the device didn't have Google Play Services updated.

dt0
  • 713
  • 2
  • 8
  • 18