Reputation: 882
I've met this problem on Android 4.2. I have three devices, one has 4.1.1 on it, a two another have 4.2. On 4.1 phone getSupportFragmentManager().getMap() returns a map, I can see it, I can find my location and so on. But on two another it returns null.
Here's my code on map setup:
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
mMap.setOnMyLocationChangeListener(this);
On 4.2 each of last three lines throws NullPointerException. On 4.1 all of them work.
I can't find, why it happens.
PS: Google maps works great on 4.2. And "My location" button works too. PS2: In the settings "Access my location", "GPS Sattelites" and "Wi-Fi & mobile network location" are turned on.
Upvotes: 1
Views: 2279
Reputation: 785
Google now made a more convenient way to get the map using the following method
myMapFragment.getMapAsync(new OnMapReadyCallback) {
@Override
public void onMapReady(GoogleMap googleMap) {
myMap = googleMap;
// Put your code here
}
});
Upvotes: 1
Reputation: 2208
Check if Google Play Services is installed. And may be you need to wait for some time before Map controller is inited.
mHandler = new Handler();
mHandler.post(new Runnable() {
@Override
public void run() {
GoogleMap map = getMap();
if (map != null) {
map.setMyLocationEnabled(true);
// INIT HERE
map.getUiSettings().setMyLocationButtonEnabled(false);
// ...
} else mHandler.post(this);
}
});
Upvotes: 3