android


Android MapView getMap() returns null


MapView's getMap() method can return null. I know this is an intended behavior by Google.
Can someone provide a definitive description as to when and under which circumstances the getMap() method returns null?
I know that if Google Services are unavailable on the given device, getMap() will return null. This eventuality is relatively well documented. I'm more concerned with the vague other case where even when Google Services are installed on a device, getMap() can still return null.
My assumption up to this point is that there is some initialization of the underlying maps system, during which time your code might execute and get a null map.
Am I correct in my assumption?
Is there any particular place in the Activity or Fragment lifecycle where we can definitively get a non-null GoogleMap (if we assume Google Services IS installed)?
My goal in asking this question is to prevent a litany of if(mapView.getMap() != null) checks littered throughout my code. In addition, this question seems to still come up on a regular basis here on StackOverflow and I'd like to see if we can flesh out the truth behind what exactly is going on with MapView and getMap()
Scenarios handled by Google
If the google play apk is not installed you will get a null. But its somehow built in to download it from the playstore. But the playstore may not be installed so you get a null map. If the internet isn't available and the playstore is installed but google play services is not you get a null because google play services can't be installed. But coding for these senarios is done for you.
Scenarios handled by your code.
This is what you have to code for. If you deviate from the pattern outlined in the map samples you will get a null map. Every sample does it the same way. This is so if the first call in onCreate fails there is still a second chance to get the map in onResume. Your solution is to call getMap() only in one place and save the map object that is returned. Follow the basic pattern that the map samples use. In the onCreate and onResume methods you call setupmapifneeded which inturn calls setupmap when the map object is not null.
Here's the first map sample.
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gosylvester.testapp;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
/**
* This shows how to create a simple activity with a map and a marker on the map.
* <p>
* Notice how we deal with the possibility that the Google Play services APK is not
* installed/enabled/updated on a user's device.
*/
public class BasicMapActivity extends FragmentActivity {
/**
* Note that this may be null if the Google Play services APK is not available.
*/
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_demo);
setUpMapIfNeeded();
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
/**
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
* installed) and the map has not already been instantiated.. This will ensure that we only ever
* call {#link #setUpMap()} once when {#link #mMap} is not null.
* <p>
* If it isn't installed {#link SupportMapFragment} (and
* {#link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
* install/update the Google Play services APK on their device.
* <p>
* A user can return to this FragmentActivity after following the prompt and correctly
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not have been
* completely destroyed during this process (it is likely that it would only be stopped or
* paused), {#link #onCreate(Bundle)} may not be called again so we should call this method in
* {#link #onResume()} to guarantee that it will be called.
*/
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
/**
* This is where we can add markers or lines, add listeners or move the camera. In this case, we
* just add a marker near Africa.
* <p>
* This should only be called once and when we are sure that {#link #mMap} is not null.
*/
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
Google has a more convenient way to get the map using the following method, because it might depend from a device to device to get the map ready!
Use the following method to get the map when it is ready.
myMapFragment.getMapAsync(new OnMapReadyCallback) {
#Override
public void onMapReady(GoogleMap googleMap) {
myMap = googleMap;
}
});
I have an Android Tab3 Running Android 4.2.2
All I had to do is update google maps and it did the trick.

Related Links

Android - How to show Facebook and Twitter's login screen using Intent?
Auto scroll list to last view position Android
webview links not working
Android. Making the screen go black
How can I receive notifications from GCM when application is stopped
Bluetooth SPP and other Bluetooth Services
What is the optimal bitmap size for drawable ldpi / mdpi / hdpi / xhdpi folders?
How to set text color of Gridview
How to read XML file tag by tag in Android?
Rregister a BroadcastReceiver in Titanium Appcelerator
How to change size of text in application settings?
com.android.ddmlib.SyncException: No space left on device (physical phone not emulator)
Android: How do I create loading screen while access the database
WebView Click Action
android Layout weight programatically
How to get DTEND and DTSTART from UTC millis since epoch to human readable time?

Categories

HOME
cygwin
kentico
localization
apache-jena
css3
package
rendering
opendj
soa
telegram-bot
zip
special-characters
commonjs
google-chrome-app
heap
filechannel
php-5.6
google-form
supervisor
gkturnbasedmatch
renjin
parameter-passing
modbus-tcp
alpacajs
asp.net-mvc-viewmodel
xmonad
mapstruct
gmm
undertow
joptionpane
exit
progress-bar
quantlib
eviews
hibernate-validator
github3.py
fossil
batching
docfx
amazon-ses
hackintosh
ogc
android-exoplayer
file-sharing
show-hide
jboss-4.2.x
dotnet-httpclient
dtsearch
semantic-logging
git-flow
dt
gpg-signature
py2neo
universal-analytics
gameplay-kit
rsa-archer-grc
cudd
centrifuge
swizzling
cfchart
omniauth-facebook
bizagi
sklearn-pandas
uiactionsheet
findfirst
jparsec
relativelayout
dynamics-nav-2016
r-package
oracle-spatial
lazybones
jdi
sql-server-data-tools
linked-tables
dnssec
node-serialport
mmwormhole
exceldatareader
iiop
raygun
websphere-esb
bin
embedded-fonts
web-farm
dajaxice
rmiregistry
josql
jsr286
floating-point-exceptions
catransform3d
facebook-iframe
web-garden
web-application-project
asynchronous-wcf-call
entitykey
flvplayback

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App