【问题标题】:getSupportFragmentManager().findFragmentById returns null for google maps in fragment in android?getSupportFragmentManager().findFragmentById 为 android 片段中的谷歌地图返回 null?
【发布时间】:2016-11-19 04:15:59
【问题描述】:

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

代码如下:

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:

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 地图,它可以使用代码:

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

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

我试过了:

  • 在“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 都为空。

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

【问题讨论】:

  • 您必须使用onCreateView 来表示片段。你能把日志贴在这里让我们知道错误是什么吗?
  • 如果我使用onCreateView 我仍然会收到此错误。您可以在我的问题更新中看到错误报告
  • 我找到了解决方案。谢谢:)

标签: android google-maps android-fragments fragment


【解决方案1】:

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

删除 Fragment 中的 onCreate() 覆盖,并添加一个 onCreateView() 覆盖,您可以在其中扩展布局并调用 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;
}

【讨论】:

  • Tnx 回复,但此代码也不起作用。 findFragmentById 仍然返回 null。
  • 完成。再次Tnx :)
  • @DanielNugent 现在工作正常,我用的是 ButterKnife,它解决了问题
  • 我遇到了同样的问题。我花了很长时间才看到你的回答。多亏了你,它奏效了。谢谢。
  • @deadfish 这就是他们设置框架的方式。如果您在活动中使用 SupportMapFragment,则可以只使用活动片段管理器。如果您在片段内部使用 SupportMapFragment,那么您就有嵌套片段,您需要使用外部片段的子片段管理器才能使其工作。
猜你喜欢
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
相关资源
最近更新 更多