Android MapFragment 或 MapView getMap() 在 Lollipop 上返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26592889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MapFragment or MapView getMap() returns null on Lollipop
提问by mroczis
I've been using Google Maps API v2 for a long time on Android 4.x versions without a problem. Now I installed latest Lollipop build on my Nexus devices (5 and 7) trying to materialize the app.
我在 Android 4.x 版本上使用 Google Maps API v2 已经很长时间了,没有任何问题。现在我在我的 Nexus 设备(5 和 7)上安装了最新的 Lollipop 版本,试图实现该应用程序。
I'd like to point out that everything is ok on KitKiat and the problem I'm describing is poping up only on Lollipop.
我想指出在 KitKiat 上一切正常,而我所描述的问题仅在 Lollipop 上出现。
In my XML source code I'm using MapFragment (Google Play Services library version 6.1.11).
在我的 XML 源代码中,我使用的是 MapFragment(Google Play 服务库版本 6.1.11)。
<fragment android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
In Java code I'm overriding OnPause() method to reach map:
在 Java 代码中,我覆盖 OnPause() 方法来访问地图:
GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
At this line it throws NullPointerException. In debugger app is able to find fragment, however it's not able to return GoogleMap. I've also tried to use MapView. It also throws null. The weirdest thing for me is that map loads without a problem on app itself but in code I cant reach it to work with it.
在这一行,它抛出 NullPointerException。在调试器应用程序中能够找到片段,但它无法返回 GoogleMap。我也试过使用 MapView。它也会抛出空值。对我来说最奇怪的是地图加载在应用程序本身上没有问题,但在代码中我无法使用它来使用它。
回答by Rhisiart
I had exactly the same problem but this is what worked for me:
我遇到了完全相同的问题,但这对我有用:
Replace this...
换这个...
GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
with this...
有了这个...
GoogleMap map = getMapFragment().getMap();
GoogleMap map = getMapFragment().getMap();
then slip this bad boy in and give it a whirl...
然后把这个坏男孩放进去试一试......
private MapFragment getMapFragment() {
FragmentManager fm = null;
Log.d(TAG, "sdk: " + Build.VERSION.SDK_INT);
Log.d(TAG, "release: " + Build.VERSION.RELEASE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Log.d(TAG, "using getFragmentManager");
fm = getFragmentManager();
} else {
Log.d(TAG, "using getChildFragmentManager");
fm = getChildFragmentManager();
}
return (MapFragment) fm.findFragmentById(R.id.map);
}
回答by Exception
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;
}
});
回答by bubastis
It looks like this might be an issue with a targetSdkVersion of 21: https://code.google.com/p/android-developer-preview/issues/detail?id=1947
看起来这可能是 targetSdkVersion 为 21 的问题:https: //code.google.com/p/android-developer-preview/issues/detail?id =1947
However, switching to getChildFragmentManager() worked for me:
但是,切换到 getChildFragmentManager() 对我有用:
findFragmentById for SupportMapFragment returns null in Android Studio
用于 SupportMapFragment 的 findFragmentById 在 Android Studio 中返回 null
回答by iGoDa
Have you tried isGooglePlayServicesAvailableto check why its returning null? null has many reasons on getmap, try using this to check why its giving null
您是否尝试过isGooglePlayServicesAvailable来检查它为什么返回 null?null 在 getmap 上有很多原因,尝试使用它来检查为什么它给出 null