Android的图形页面的GetMap()返回null [英] Android MapView getMap() returns null

查看:181
本文介绍了Android的图形页面的GetMap()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图形页面的GetMap()方法可以返回null。我知道这是谷歌的预期行为。

MapView's getMap() method can return null. I know this is an intended behavior by Google.

有人可以提供一个明确说明何时以及在何种情况下的的GetMap()方法返回null?

Can someone provide a definitive description as to when and under which circumstances the getMap() method returns null?

我知道,如果谷歌服务是在指定设备上不可用,的GetMap()将返回null。这种可能性是比较有据可查。我更关心的是在那里,即使谷歌服务都安装在设备上的模糊等情况,的GetMap()仍然可以返回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.

我的假设到这一点的是,有一些初始化的基本地图系统,在此期间,您的code可能会执行,并得到一个空的地图。

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.

我是正确的我的假设?

有没有在活动或片段生命周期的任何特别的地方,我们可以明确地得到一个非空 GoogleMap的(如果我们假设安装了谷歌服务)?

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)?

我在问这个问题的目标是prevent的一连串的如果(mapView.getMap()!= NULL)检查散落在我的code 。此外,这个问题似乎依然拿出定期在这里StackOverflow的,我想看看我们是否能够割肉出局的背后究竟是用图形页面的GetMap()

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()

推荐答案

由谷歌处理的场景 如果没有安装谷歌播放apk文件,你会得到一个空。但它以某种方式建在从playstore下载。但playstore可能无法安装,所以你得到一个空地图。如果网络不可用,并且安装在playstore但谷歌播放服务是不是你会得到一个空的,因为谷歌播放服务无法安装。但是编码这些senarios做是为了你。

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.

您code处理方案。 这就是你必须$ C $下。如果从地图样本中列出的模式有所差异,你会得到一个空的地图。每个样品做同样的方式。这是如此,如果在的onCreate第一次呼叫失败还有第二次机会来获得地图onResume。你的解决方法是调用的GetMap()只在一个地方,并保存返回的地图对象。按照该图示例使用的基本格局。在的onCreate onResume 方法调用 setupmapifneeded 这inturn调用 setupmap 时,地图对象不为空。

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.

这是第一个图来样。

/*
 * 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"));
    }
}

这篇关于Android的图形页面的GetMap()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆