Problem loading Maps v2

0

Good morning!

I have the following code to load the map into my app:

if (mMap == null){
    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    if(null == mMap)
    {
        SupportMapFragment fragment = SupportMapFragment.class.cast(getSupportFragmentManager().findFragmentById(R.id.map));
        fragment.onCreate(savedInstanceState);
        fragment.getMapAsync(this);
    }
}

XML:

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

Apparently, the tests worked correctly, there was no error loading, but I'm getting several failures in play.google :

java.lang.NullPointerException: Attempt to read from field 'com.google.android.gms.maps.model.LatLng com.google.android.gms.maps.model.MarkerOptions.b' on a null object reference
    at com.google.maps.api.android.lib6.d.et.a(Unknown Source)
    at com.google.android.gms.maps.internal.j.onTransact(SourceFile:167)
    at android.os.Binder.transact(Binder.java:395)
    at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addMarker(Unknown Source)
    at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)

The NullPointerException occurs because, when trying to add the marker on the map, it is null.

I tried the problem so that NullPointerException does not occur, but would you like to know why the map is not loaded?

From what I've noticed, they occur in all versions of Android (from 4.0 to 5.1), it's nothing specific to a version.

Are there other ways to load the map?

Thank you in advance!

Greetings

    
asked by anonymous 13.10.2015 / 14:35

1 answer

1

It's nice that you use getLastLocation to retrieve the value of your position, check if gps is enabled, and doubtfully create a LatLng obj with a value of "0,0", it's probably trying to create the marker in Activity's onCreate, this causes it to grab the null object before it can get a valid location, anything from a look at the link ( Getting the Last Location - Android )

    
14.10.2015 / 19:36