首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Google Maps Android API v2抛出GooglePlayServicesNotAvailableException,过期,SupportMapFragment.getMap()返回null

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

Stack Overflow用户
提问于 2012-12-05 19:24:57
回答 3查看 29.3K关注 0票数 18

我正在尝试将新的Google Maps Android API v2整合到一个使用Android Support Library的应用程序中。我已经经历了许多次迭代,试图找到工作的地方,现在我已经放弃了,并认为我应该在这里询问。

我转到应用程序接口控制台,创建了一个项目,启用了Google Maps API v2 for Android,根据需要创建了一个调试密钥,并将其粘贴到清单中。没有身份验证问题或类似的问题。我很确定我做得对。

在我的项目的libs文件夹中,我放置了android-support-v4.jar (通过项目右键->Android->Add Support Library...)。在Eclipse中,我执行了文件->导入->安卓->现有安卓代码到工作区,并导入了android-sdk/google/google_play_services/libproject/google-play-services_lib.的最新版本(版本3然后,我将其作为依赖库添加到我的项目中(项目右键单击->Properties->Android->将其作为库依赖项添加到底部。

在我的清单中,我有:

代码语言:javascript
运行
复制
<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>

在我的mainview.xml中,我有:

代码语言:javascript
运行
复制
<fragment
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/map"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   class="com.google.android.gms.maps.SupportMapFragment" />

现在在MainView.java上我有:

代码语言:javascript
运行
复制
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class MainView extends FragmentActivity ... {
    private GoogleMap mMap;

    @Override
    protected void onResume() {
        ...
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.map);
        SupportMapFragment mapFragment = (SupportMapFragment)fragment;
        mMap = mapFragment.getMap();
        //PROBLEM: mMap is null here even though mapFragment is not
    }
}

然后我想我可能需要初始化片段,所以我在设置了内容视图之后添加了我的onCreate

代码语言:javascript
运行
复制
//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中我引用了:

代码语言:javascript
运行
复制
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类,所以我想也许我应该先调用它。

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

代码语言:javascript
运行
复制
try {
    MapsInitializer.initialize(this);
} catch (GooglePlayServicesNotAvailableException e) {
    e.printStackTrace();
}

使用此日志报告警告(第313行是MapsInitializer.initialize(this)):

代码语言:javascript
运行
复制
com.google.android.gms.common.GooglePlayServicesNotAvailableException
    at com.google.android.gms.internal.n.c(Unknown Source)
    at com.google.android.gms.internal.n.a(Unknown Source)
    at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
    at myapp.activities.MainView.inflateSelectedViewAndSetData(MainView.java:313)
    at myapp.activities.MainView.onClick(MainView.java:642)
    at android.view.View.performClick(View.java:3153)
    at android.view.View$PerformClick.run(View.java:12148)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:152)
    at android.app.ActivityThread.main(ActivityThread.java:4615)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
    at dalvik.system.NativeStart.main(Native Method)

在我的所有尝试中,Logcat将报告以下警告:

代码语言:javascript
运行
复制
Google Play services out of date.  Requires 2010100 but found 1013

这可能是罪魁祸首,但我找不到解决办法。

在这一点上,我没有想法。我读了几个帖子hereherehere,但是没有一个建议解决了这个问题。对于后者,他们建议不包括项目依赖项,而只包括jar文件。好吧,无论是使用google-play-services_lib/libs文件夹中的google-play-services.jar还是从该项目中导出我自己的jar,这都不起作用。

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

对您的帮助,我们深表感谢。:)

更新:

下面是量子比特的解决方案。

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-12-05 19:43:21

从Play Store下载并安装最新的Google Play services。由于某种原因,我无法通过搜索找到它。通过运行/extras/google/google_play_services/samples/maps文件夹中SDK中的示例应用程序,系统会提示我安装升级,并将我带到Play Store来执行此操作。

票数 5
EN

Stack Overflow用户

发布于 2013-03-06 22:42:01

如该链接底部所述;http://developer.android.com/google/play-services/setup.html

下面是正确处理这个问题的示例代码;

代码语言:javascript
运行
复制
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();
    }
票数 15
EN

Stack Overflow用户

发布于 2013-03-19 22:14:37

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

示例中的代码:

代码语言:javascript
运行
复制
@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"));
}

和结果:

作为文档的悲哀

只有在加载了底层地图系统并且片段中存在底层视图时,才能使用getMap()获取GoogleMap。这个类会自动初始化地图系统和视图;但是,不能保证它什么时候会准备好,因为这取决于Google Play服务APK的可用性。如果GoogleMap不可用,getMap()将返回null。SupportMapFragment

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13722192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档