Map fragment V2 in a Fragment

Furqan picture Furqan · Mar 9, 2013 · Viewed 8.4k times · Source

I am facing some issue"Null pointer Expcepttion at getmap()" in Google Map Fragments . I want to insert mapfragment in a fragment Please review the code and let know whether I am taking the right appraoch or not .. and let me know what exactly I need to do with my code to make it work

My Layout file :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" 
  android:orientation="vertical"
  android:background="@drawable/bg2"
  android:weightSum="100">

<Button
    android:id="@+id/perx_button"
    android:layout_marginTop="70dip"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/perx_card_btn" />

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

</LinearLayout>


public class PerxDetailedFragment extends SupportMapFragment {

  static final LatLng HAMBURG = new LatLng(53.558, 9.927);
  static final LatLng KIEL = new LatLng(53.551, 9.993);
 GoogleMap map;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

     FragmentTransaction ft = getFragmentManager().beginTransaction();

    map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();

    SupportMapFragment fmap = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);

    if (fmap == null) {
        fmap = SupportMapFragment.newInstance();
        ft.add(R.id.map, fmap);
    }

    ft.commit();

    Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
            .title("Hamburg"));
        Marker kiel = map.addMarker(new MarkerOptions()
            .position(KIEL)
            .title("Kiel")
            .snippet("Kiel is cool")
            .icon(BitmapDescriptorFactory
                .fromResource(R.drawable.ic_launcher)));

        // Move the camera instantly to hamburg with a zoom of 15.
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

        // Zoom in, animating the camera.
        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);


     return inflater.inflate(R.layout.perx_detailed, null, false);
}
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);       


    }


}

Answer

erickj00001 picture erickj00001 · Mar 12, 2013

You need to inflate the layout first. The problem is you're doing that at the end of onCreateView, so the map fragment isn't there when you call findFragmentById.

Also, PerxDetailedFragment should extend Fragment, not SupportMapFragment.

public class PerxDetailedFragment extends Fragment {
    // ...

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.perx_detailed, null, false);

        map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();

        //...

        return v;
    }