我正在尝试使用 com.google.android.gms.maps.SupportMapFragment 加载地图,但我在 findFragmentById 中得到空值。
我的 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
}
}