1

This works on api 12 and above but on 11 and below crash.

This is my class:

public class Home extends Fragment {

    SupportMapFragment mMapView;
    GoogleMap googleMap;

    final long tiempo = 5 * 1000;


    double latitude,longitude;

    public View view;

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



        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null)
                parent.removeView(view);
        }
        try {
            view = inflater.inflate(R.layout.fragment_home, container, false);
        } catch (InflateException e) {
        }


        GPSTracker gps = new GPSTracker(getActivity());
        if(gps.canGetLocation()){
            latitude = gps.getLatitude();
            longitude = gps.getLongitude();
            mMapView = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapa);
            mMapView.onCreate(savedInstanceState);

            mMapView.onResume();// needed to get the map to display immediately


            googleMap = mMapView.getMap();




        }else{
            gps.showSettingsAlert();
        }
        return view;

    }
}

THIS my XML

<com.google.android.gms.maps.SupportMapFragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapa"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

This is the error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{.MainActivity}: java.lang.NullPointerException
T3KBAU5
  • 1,861
  • 20
  • 26
  • Since you didn't specify in which line it is crashing, I'm going to guess that it's the `getSupportFragmentManager`. I had a similar issue a time ago and I fixed it following this thread http://stackoverflow.com/questions/26597161/findfragmentbyid-for-supportmapfragment-returns-null-in-android-studio – Eric Martinez Jun 14 '15 at 03:09

0 Answers0