NullPointerException error in setMyLocationEnabled ()

1

I'm developing an app whose main function is to capture the user's location. I have already made a code of the error, I do not know if it is complete, I followed following internet examples:

Code:

private GoogleMap mMap;

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
    fragment.getMapAsync(this);

    return rootView;
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap.setMyLocationEnabled(true);


}

Run-time error:

Process: com.compscitutorials.basigarcia.navigationdrawervideotutorial, PID: 16457
   java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setMyLocationEnabled(boolean)' on a null object reference
       at br.com.projeto.caminhossembarreiras.MainFragment.onMapReady(MainFragment.java:37)
       at com.google.android.gms.maps.MapFragment$zza$1.zza(Unknown Source)
       at com.google.android.gms.maps.internal.zzl$zza.onTransact(Unknown Source)
       at android.os.Binder.transact(Binder.java:387)
       at com.google.android.gms.maps.internal.v$a$a.a(:com.google.android.gms.alldynamite:82)
       at maps.ei.bu$6.run(Unknown Source)
       at android.os.Handler.handleCallback(Handler.java:746)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5443)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Line error:

 mMap.setMyLocationEnabled(true);
    
asked by anonymous 30.03.2016 / 07:29

1 answer

0

The onMapReady method returns the map loaded as a parameter ( googleMap ).

You need to pass the reference to mMap !

Try the following:

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap  = googleMap;
    mMap.setMyLocationEnabled(true);
}
    
24.08.2016 / 19:50