Android片段Google Maps V2:膨胀类片段时出错

Android片段Google Maps V2:膨胀类片段时出错,android,google-maps,android-fragments,Android,Google Maps,Android Fragments,我正在开发一个使用fragment tab的应用程序,我的一个fragment使用Google Maps V2 public class AppMapFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.infl

我正在开发一个使用fragment tab的应用程序,我的一个fragment使用Google Maps V2

public class AppMapFragment extends Fragment { 

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.fragment_map, container, false); 

    try { 
        // Loading map 
        initilizeMap(); 

    } catch (Exception e) { 
        e.printStackTrace(); 
    } 
    initializePOI(); 

    addMarkers(); 
    googleMap.setMyLocationEnabled(true); 
    googleMap.getUiSettings().setMyLocationButtonEnabled(true); 
    LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    String provider = locationManager.getBestProvider(criteria,true); 
    Location location = locationManager.getLastKnownLocation(provider); 
    if (location!=null) { 
            double latitude = location.getLatitude(); 
            double longitude = location.getLongitude(); 
            myPos = new LatLng (latitude, longitude); 
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myPos, 15)); 
    } else { 
        // Move the camera instantly to London with a zoom of 15. 
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LONDON, 15)); 
    } 

    // Zoom in, animating the camera. 
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 

    return rootView; 
} 
我的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#17df0d">

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

</RelativeLayout>

我只复制了部分错误,因为它相当长:(.每当我尝试运行应用程序时,就会发生此错误。它引用的行是
View rootView=inflater.inflate(R.layout.fragment\u map,container,false)
。任何帮助都将不胜感激!请提前感谢

查看此实现,您应该像以前一样,创建一个带有映射的片段:

public class MapFragment extends Fragment
创建布局: mapview.xml:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapview"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraZoom="12"
map:mapType="normal"
map:uiZoomControls="false"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiZoomGestures="true"
map:uiTiltGestures="false" />
然后,要获取映射实例,请执行以下操作:

private GoogleMap getGoogleMap() {
        if (map == null && getActivity() != null && getActivity().getSupportFragmentManager()!= null) {
            SupportMapFragment smf = (SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.mapview);
            if (smf != null) {
                map = smf.getMap();
            }
        }
        return map;
    }

我只是想知道为什么你想在另一个片段中膨胀一个MapFragmet(它已经是一个片段了)。我的应用程序包括五个选项卡,其中一个是地图。我认为这样做的方法是:(
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapview"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraZoom="12"
map:mapType="normal"
map:uiZoomControls="false"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiZoomGestures="true"
map:uiTiltGestures="false" />
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    return inflater.inflate(R.layout.mapview, container, false);
}
private GoogleMap getGoogleMap() {
        if (map == null && getActivity() != null && getActivity().getSupportFragmentManager()!= null) {
            SupportMapFragment smf = (SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.mapview);
            if (smf != null) {
                map = smf.getMap();
            }
        }
        return map;
    }