【问题标题】:android getActivity().getSupportFragmentManager().findFragmentById is nullandroid getActivity().getSupportFragmentManager().findFragmentById 为空
【发布时间】:2015-02-23 17:05:36
【问题描述】:

我正在尝试使用 com.google.android.gms.maps.SupportMapFragment 加载地图,但我在 findFragmentById 中得到了 null。

我的xml片段文件:

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

MainActivity.java:

public class MainActivity extends ActionBarActivity{
    private void selectItem(int position){
        //some code
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, new MyMapFragment).commit();
    }
}

地图片段:

MyMapFragment extends android.support.v4.app.Fragment{
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        SupportMapFragment smf = (SupportMapFragment)  getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
        //here smf is null
    }
}

【问题讨论】:

标签: android android-fragments android-maps android-maps-v2


【解决方案1】:

This is the answer:

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

此外,在玩嵌套片段时,请使用getChildFragmentManager()

【讨论】:

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