【问题标题】:getSupportFragmentManager().findFragmentById(R.id.map) started returning nullgetSupportFragmentManager().findFragmentById(R.id.map) 开始返回 null
【发布时间】:2015-03-31 12:56:15
【问题描述】:

我在另一个片段中有一个地图片段。这以前有效,但我认为在我更新了谷歌播放服务库后它搞砸了。怎么了? 片段映射.xml:

<fragment
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

地图片段:

GoogleMap map;
private static View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);
    }
    try {

        view = inflater.inflate(R.layout.fragment_map, container, false);
        map = ((SupportMapFragment) getActivity()
                .getSupportFragmentManager()
                .findFragmentById(R.id.map))
                .getMap(); // NullPointerException at this line
        map.getUiSettings().setAllGesturesEnabled(true);
        map.getUiSettings().setMyLocationButtonEnabled(false);
        map.getUiSettings().setZoomControlsEnabled(true);
    } catch (InflateException e) {
        /* map is already there, just return view as it is */
    }

    return view;
}

【问题讨论】:

    标签: android google-maps google-play-services


    【解决方案1】:

    我必须改变

    getActivity().getSupportFragmentManager()
    

    getChildFragmentManager()
    

    让它工作。不知道为什么它在几天前运行良好。

    【讨论】:

      【解决方案2】:

      This is the answer:

      注意:当布局包含片段时,您不能将布局扩展为片段。只有动态添加到片段时才支持嵌套片段。

      【讨论】:

        【解决方案3】:

        在 onCreateView 中,您不应该这样做。使用官方文档中建议的方法进行片段间通信。在这里,我认为您的问题与时间有关。并非所有片段都是同步创建或管理的。另外,我不太确定你是否可以从 onCreateView() 调用 getActivity() - 因为片段只有在 onCreateView() 之后才会得到 onActivityCreated() 回调。

        【讨论】:

          猜你喜欢
          • 2023-03-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多