Supportmapfragment.Getmap() Returns Null

SupportMapFragment.getmap() returns null

map takes some time to load, so you need to run your code in handler -->

Handler handler = new Handler();
handler.postDelayed(new Runnable()

@Override
public void run() {
GoogleMap googleMap = SupportMapFragment.newInstance(new GoogleMapOptions().zOrderOnTop(true)).getMap();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.map_content, fragment);
ft.commit();
if(googleMap != null) {
googleMap.addMarker(new MarkerOptions().position(result)).setVisible(true);

// Move the camera instantly to location with a zoom of 15.
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(result, 15));

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

googleMap.getUiSettings().setZoomControlsEnabled(false);
googleMap.getUiSettings().setCompassEnabled(false);
googleMap.getUiSettings().setMyLocationButtonEnabled(false);

handler.removeCallbacksAndMessages(null);
}

else {
handler.postDelayed(this, 500);
}
}, 500);

MapFragment or MapView getMap() returns null on Lollipop

I had exactly the same problem but this is what worked for me:

Replace this...

GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

with this...

GoogleMap map = getMapFragment().getMap();

then slip this bad boy in and give it a whirl...

private MapFragment getMapFragment() {
FragmentManager fm = null;

Log.d(TAG, "sdk: " + Build.VERSION.SDK_INT);
Log.d(TAG, "release: " + Build.VERSION.RELEASE);

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Log.d(TAG, "using getFragmentManager");
fm = getFragmentManager();
} else {
Log.d(TAG, "using getChildFragmentManager");
fm = getChildFragmentManager();
}

return (MapFragment) fm.findFragmentById(R.id.map);
}

getMap() is null in SupportMapFragment in google map in android fragment

I got the reason for it, I think I should post for future readers......

Actually as I GoogleMap was called in a fragment so, in the line..

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

instead of

getFragmentManager(), getSupportFragmentManager() should be used in fragments.

Corrected one-

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

Google Maps getMap returns null

Since the 6.5 version of Play Services Google recommends using of asynchronous getting of GoogleMap and getMap() is now deprecated. As stkent's mentioned you have to call getMapAsync(this).

As this method asynchronous you need some callback for this method, that's why also you have to make your fragment implementing OnMapReadyCallback and in the method onMapReady() of these callbacks you can get reference to the map.

Also change line ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this); to ((SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map)).getMapAsync(this); 'cause inside fragment you have to use ChildFragmentManager.

Google Play Service - SupportMapFragment.getMap() always returning null

As CommonsWare stated in the linked question, the problem only occures when creating the SupportMapFragment programmatically and not a <fragment> XML tag.

If created programmatically, the map will be available in the onActivityCreated() call. So my workaround is the following:

mMapFragment = new SupportMapFragment() {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
GoogleMap map = mMapFragment.getMap();
if (map != null) {
//Your initialization code goes here
}
}
};

MapFragment getMap() return null (mapBox)

Why are you extending MapFragment and defining your own layout? Anyway this looks like there is no R.id.mapView in your R.layout.fragment_map. Can't really say more without the full layouts and fragment loading logic.

Here is some code that works (using mapbox SDK 2.2, Android SDK 23):

MainActivity class:

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
protected void onStart() {
super.onStart();

// Load the fragment dynamically
getFragmentManager()
.beginTransaction()
.add(R.id.fragment_container_layout, MyMapFragment.create())
.commit();
}

}

Custom Fragment:

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.views.MapView;

public class MyMapFragment extends Fragment {
private MapView mMapView;

public static MyMapFragment create() {
return new MyMapFragment();
}

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

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

mMapView = (MapView) rootView.findViewById(R.id.mb_map_view);

mMapView.setStyleUrl(Style.MAPBOX_STREETS);
mMapView.setCenterCoordinate(new LatLng(40.956645, 14.304816));
mMapView.setZoomLevel(11);
mMapView.onCreate(savedInstanceState); // If this line is omitted there is a native crash in the MapBox library

return rootView;
}

@Override
public void onStart() {
super.onStart();
mMapView.onStart();
}

@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}

@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}

@Override
public void onStop() {
super.onStop();
mMapView.onStop();
}

@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
}

activity_main.xml (layout for main activity):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout
android:id="@+id/fragment_container_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</FrameLayout>

my_map_fragment.xml (layout for custom map fragment):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.mapbox.mapboxsdk.views.MapView
android:id="@+id/mb_map_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:access_token="@string/mb_token"/>
</FrameLayout>

in google maps V2 ... fragment.getMap() returns null

You are creating a dynamic fragment, via a FragmentTransaction. When you call commit(), the fragment has not yet been added to the screen, because the FragmentTransaction has only been scheduled to occur -- it has not occurred yet. Hence, the SupportMapFragment has not been called with onCreateView() yet, so there is no GoogleMap.

Either switch to static fragments (<fragment> tag in a layout), or delay your use of the GoogleMap until after the transaction has been processed.

findFragmentById for SupportMapFragment returns null in Android Studio

Judging by the fact that you're overriding Fragment.onActivityCreated(), I take it that your layout containing the map fragment is the layout for your Fragment. In that case, the SupportMapFragment is a child fragment of your hosting Fragment. When you attempt to retrieve it, you're using the Activity FragmentManager. You should instead use your Fragment's FragmentManager:

For example, this:

SupportMapFragment m = ((SupportMapFragment) getActivity()
.getSupportFragmentManager().findFragmentById(R.id.safety_map));

becomes:

SupportMapFragment m = ((SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.safety_map));


Related Topics



Leave a reply



Submit