Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google Maps Android API v2抛出GooglePlayServicesNotAvailableException,过期,SupportMapFragment.getMap()返回null_Android_Google Maps_Android Support Library_Google Play Services_Google Maps Android Api 2 - Fatal编程技术网

Google Maps Android API v2抛出GooglePlayServicesNotAvailableException,过期,SupportMapFragment.getMap()返回null

Google Maps Android API v2抛出GooglePlayServicesNotAvailableException,过期,SupportMapFragment.getMap()返回null,android,google-maps,android-support-library,google-play-services,google-maps-android-api-2,Android,Google Maps,Android Support Library,Google Play Services,Google Maps Android Api 2,我正在尝试使用Android支持库将新的Google Maps Android API v2整合到一个应用程序中。 我已经经历了多次迭代,试图找到工作的地方,现在我已经放弃了,我想我应该在这里问一下 我去了API控制台,创建了一个项目,为Android启用了Google Maps API v2,根据需要创建了一个调试键,并将其粘贴到清单上。没有身份验证问题或类似的问题。我肯定我做对了 在我的项目的libs文件夹中,我放置了android-support-v4.jar(通过项目右键单击->andr

我正在尝试使用Android支持库将新的Google Maps Android API v2整合到一个应用程序中。 我已经经历了多次迭代,试图找到工作的地方,现在我已经放弃了,我想我应该在这里问一下

我去了API控制台,创建了一个项目,为Android启用了Google Maps API v2,根据需要创建了一个调试键,并将其粘贴到清单上。没有身份验证问题或类似的问题。我肯定我做对了

在我的项目的
libs
文件夹中,我放置了
android-support-v4.jar
(通过项目右键单击->android->添加支持库…)。在Eclipse中,我将文件->导入->安卓->现有安卓代码导入到工作区,并导入了最新的(rev 3)安卓sdk/google/google\u play\u services/libproject/google-play-services\u lib。然后我将其作为依赖项库添加到我的项目中(项目右键单击->属性->Android->在底部将其作为库依赖项添加)

在我的清单中,我有:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>
    <permission
      android:name="myapp.permission.MAPS_RECEIVE"
      android:protectionLevel="signature"/>

    <All the uses-permission required like INTERNET, ACCESS_NETWORK_STATE, WRITE_EXTERNAL_STORAGE, FINE AND COARSE LOCATION, etc>
    <uses-permission android:name="myapp.permission.MAPS_RECEIVE"/>
    <uses-feature android:glEsVersion="0x00020000" android:required="true"/>
    <application ...>
        ...
        <uses-library android:name="com.google.android.maps" />
        <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AI..."/>
    </application>
然后我想我可能需要初始化片段,所以在设置内容视图后,我在我的
onCreate
中添加了:

//Fragments
FragmentManager manager = getSupportFragmentManager(); 
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.map, SupportMapFragment.newInstance());
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();
然后我说,可能不是
SupportMapFragment
,但我实际上应该参考实际的片段,在我的
onResume
中,我做了:

Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.map);
SupportMapFragment mapFragment = (SupportMapFragment)fragment;

//Fragments
FragmentManager manager = getSupportFragmentManager(); 
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.map, mapFragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();

mMap = mapFragment.getMap();
mMap
在此之后再次为空

然后我看到有一个
MapsInitializer
类,所以我想我应该先调用它

因此,在获取上述代码中的片段之前,我添加了代码:

try {
    MapsInitializer.initialize(this);
} catch (GooglePlayServicesNotAvailableException e) {
    e.printStackTrace();
}
使用此Logcat报告警告(第313行是
映射初始化器。初始化(this)
):

在我的整个尝试过程中,Logcat将报告以下警告:

Google Play services out of date.  Requires 2010100 but found 1013
这可能是罪魁祸首,但我没有找到解决办法

在这一点上,我已经没有主意了。我读了好几篇文章,但没有一条建议解决了这个问题。对于后者,他们建议不包括项目依赖项,只包括jar文件。好吧,使用
google-play-services_lib/libs
文件夹中的
google-play-services.jar
也不起作用或者从那个项目中导出我自己的jar

顺便说一句,我是在实际设备(2.3.5和3.2版平板电脑)上运行的,而不是模拟器

衷心感谢您的帮助。:)

更新:

下面是量子位元的解

至于导出库依赖项,一个不必处理它的好方法(在处理存储库时这是一个难题)是使用并导出
google\u play\u services\u lib
项目(我没有从该项目创建本地副本,因为当它更新时我不想重新导入),并将输出fat jar文件作为另一个jar文件包含在项目中。 注意:在选择要包含在fat jar中的jar文件时,我必须排除
annotations.jar
文件,因为它已经被预先包含,并且会导致重复错误。
现在,我所要做的就是将
google\u play\u services\u rev\u 3.jar
包含在我的项目的
libs
文件夹中,我可以开始了。

从play商店下载并安装最新的
google play services
。由于某种原因,我无法通过搜索找到它。通过从SDK中运行文件夹
/extras/google/google\u play\u services/samples/maps
中的示例应用程序,我被提示安装升级,并被带到play Store进行安装。

如该链接底部所述

下面是正确处理此问题的示例代码

int checkGooglePlayServices =    GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
    if (checkGooglePlayServices != ConnectionResult.SUCCESS) {
    // google play services is missing!!!!
    /* Returns status code indicating whether there was an error. 
    Can be one of following in ConnectionResult: SUCCESS, SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, SERVICE_DISABLED, SERVICE_INVALID.
    */
       GooglePlayServicesUtil.getErrorDialog(checkGooglePlayServices, mActivity, 1122).show();
    }

我在我的项目中也遇到了同样的问题。解决方案非常简单,只需处理以下可能性:
((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap()
可以返回
null
。如果您将处理
null
则内置的
SupportMapFragment
将处理
Google Play服务过期。
错误,并将显示用于更新Google Play服务的地图本地化消息和按钮,就像@qubz在示例项目中所述的那样

来自示例的代码:

@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"));
}
@覆盖
受保护的void onResume(){
super.onResume();
setupmapifneed();
}
/**
*如果可能的话,设置地图(例如,Google Play services APK已正确启动)
*已安装),并且映射尚未实例化。。这将确保我们永远
*当{@link#mMap}不为null时,调用{@link#setUpMap()}一次。
*
*如果未安装{@link SupportMapFragment}(和
*{@link com.google.android.gms.maps.MapView MapView})将提示用户
*在其设备上安装/更新Google Play services APK。
*
*用户可以按照提示正确返回此FragmentActivity
*安装/更新/启用Google Play服务。由于碎片活动可能尚未完成
*在此过程中完全破坏(可能只会停止或停止
*暂停),{@link#onCreate(Bundle)}可能不会再次调用,因此我们应该在中调用此方法
*{@link#onResume()}以保证将调用它。
*/
私有void setUpMapIfNeeded(){
//执行空检查以确认尚未实例化映射。
如果(mMap==null){
//尝试从SupportMapFragment获取映射。
mMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
//检查我们是否成功获得地图。
如果(mMap!=null){
setUpMap();
}
}
}
/**
*这是我们可以添加标记或线条、添加侦听器或移动摄影机的地方。在这种情况下,我们
*在非洲附近加一个标记。
*
*当我们确定{@link#mMap}不是null时,应该只调用一次。
*/
私有void setUpMap(){
mMap.添加标记(新标记选项)
Google Play services out of date.  Requires 2010100 but found 1013
int checkGooglePlayServices =    GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
    if (checkGooglePlayServices != ConnectionResult.SUCCESS) {
    // google play services is missing!!!!
    /* Returns status code indicating whether there was an error. 
    Can be one of following in ConnectionResult: SUCCESS, SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, SERVICE_DISABLED, SERVICE_INVALID.
    */
       GooglePlayServicesUtil.getErrorDialog(checkGooglePlayServices, mActivity, 1122).show();
    }
@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"));
}