getSupportFragmentManager().findFragmentById 为android 中的片段中的谷歌地图返回null? [英] getSupportFragmentManager().findFragmentById returns null for google maps in fragment in android?

查看:64
本文介绍了getSupportFragmentManager().findFragmentById 为android 中的片段中的谷歌地图返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Google 地图时遇到问题,即 getSupportFragmentManager().findFragmentById 始终返回 null.你知道如何解决这个问题吗?

I have a problem with Google Maps, i.e. getSupportFragmentManager().findFragmentById returns always null. Do you have an idea how to solve this?

代码如下:

fragment_map.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapp.something.MapFragment">
<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />                  
</FrameLayout>

MapsFragment.java:

MapsFragment.java:

public class MapFragment extends Fragment implements OnMapReadyCallback, android.location.LocationListener

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    SupportMapFragment mapFragment = (SupportMapFragment) this.getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
...

我在 Activity 中有 Google 地图,它可以使用代码:

I have Google Maps in Activity and it works with code:

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

我正在尝试在片段中重用它,因为我需要片段中的地图,而不是活动中的地图,但它不起作用.

I am trying to reuse this in fragment, becouse I need maps in fragment, not in activity, but it doesn't work.

我试过了:

  • 在onCreateView"函数中调用此代码
  • SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
  • GoogleMap mGoogleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap(); 已弃用,应用程序崩溃
  • SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);和类似的变化,但在所有情况下,我的 mapFragment 都为 null.
  • calling this code in "onCreateView" function
  • SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
  • GoogleMap mGoogleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap(); is deprecated, and application crashes
  • SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); and similar variations, but in all cases I get null for mapFragment.

你知道我该如何解决这个问题吗?

Do you know how could I solve this problem?

推荐答案

问题是您尝试使用 Activity 的 FragmentManager,而您应该使用 Fragment 的子 FragmentManager.

The problem is that you're trying to use the Activity's FragmentManager, and you should be using the Fragment's child FragmentManager.

移除 Fragment 中的 onCreate() 覆盖,并添加一个 onCreateView() 覆盖,您可以在其中膨胀布局并调用 getMapAsync():

Remove the onCreate() override in the Fragment, and add an onCreateView() Override where you inflate the layout and call getMapAsync():

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_map, container, false);

    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);

    mapFragment.getMapAsync(this);

    return rootView;
}

这篇关于getSupportFragmentManager().findFragmentById 为android 中的片段中的谷歌地图返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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