getSupportFragmentManager().findFragmentById(R.id.map) 开始返回 null
getSupportFragmentManager().findFragmentById(R.id.map) started returning null
我在另一个片段中有一个地图片段。这以前是有效的,但我认为在我更新 google 播放服务库后它搞砸了。怎么了?
fragment_map.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;
}
在onCreateView 中,您不应该这样做。使用官方文档中建议的方法进行片段间通信。在这里,我认为您的问题与时间有关。并非所有片段都是同步创建或管理的。另外,我不太确定您是否可以从 onCreateView() 调用 getActivity() - 因为片段只会在 onCreateView() 之后获得 onActivityCreated() 回调。
我不得不改变
getActivity().getSupportFragmentManager()
至
getChildFragmentManager()
让它工作。不知道为什么它几天前工作得很好。
Note: You cannot inflate a layout into a fragment when that layout includes a fragment. Nested fragments are only supported when added to a fragment dynamically.
我在另一个片段中有一个地图片段。这以前是有效的,但我认为在我更新 google 播放服务库后它搞砸了。怎么了? fragment_map.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;
}
在onCreateView 中,您不应该这样做。使用官方文档中建议的方法进行片段间通信。在这里,我认为您的问题与时间有关。并非所有片段都是同步创建或管理的。另外,我不太确定您是否可以从 onCreateView() 调用 getActivity() - 因为片段只会在 onCreateView() 之后获得 onActivityCreated() 回调。
我不得不改变
getActivity().getSupportFragmentManager()
至
getChildFragmentManager()
让它工作。不知道为什么它几天前工作得很好。
Note: You cannot inflate a layout into a fragment when that layout includes a fragment. Nested fragments are only supported when added to a fragment dynamically.