MapFragment或图形页面的GetMap()在棒棒堂返回null棒棒、图形、页面、MapFragment

2023-09-13 00:40:29 作者:我的微笑只给懂的人看

我一直在使用谷歌地图API V2很长一段时间在Android 4.x的版本没有问题。现在,我安装了最新版本棒棒堂我的Nexus设备(5,7)试图兑现的应用程序。

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.

我想指出的是,一切正常的KitKiat和我所描述的问题只在棒棒堂坡平了。

I'd like to point out that everything is ok on KitKiat and the problem I'm describing is poping up only on Lollipop.

在我的XML源$ C ​​$ C我使用MapFragment(谷歌播放服务库版本6.1.11)。

In my XML source code I'm using MapFragment (Google Play Services library version 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"/>

在Java的code,我重写将OnPause()方法来达到图:

In Java code I'm overriding OnPause() method to reach map:

GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

目前行它将引发NullPointerException异常。在调试器的应用程序是能够找到的片段,但它无法返回的GoogleMap。我也试着使用图形页面。它还抛出空。 最奇怪的事情对我来说是地图加载不会对应用程序本身,而code一个问题,我不能达到它与它的工作。

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.

推荐答案

我有完全一样的问题,但是这是为我工作:

I had exactly the same problem but this is what worked for me:

替换此...

GoogleMap的地图=((MapFragment)getFragmentManager()findFragmentById(R.id.map)。)的GetMap();

本...

GoogleMap的地图= 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);
}