Java 如何从navegation抽屉调用FragmentActivitie

Java 如何从navegation抽屉调用FragmentActivitie,java,android,google-maps-api-3,drawerlayout,Java,Android,Google Maps Api 3,Drawerlayout,我想在第一种情况下显示一个映射,这是调用映射片段时抽屉布局的一部分。这让我犯了错误,我不知道怎么做 @覆盖 已选择NavigationDrawerItems上的公共无效(整数位置){ FragmentManager FragmentManager=getFragmentManager(); 片段=空; 开关(位置){ 案例0: fragment=new MapsActivity();//此行显示错误 打破 案例1: //实施 打破 案例2: //实施 打破 } fragmentManager.

我想在第一种情况下显示一个映射,这是调用映射片段时抽屉布局的一部分。这让我犯了错误,我不知道怎么做

@覆盖
已选择NavigationDrawerItems上的公共无效(整数位置){
FragmentManager FragmentManager=getFragmentManager();
片段=空;
开关(位置){
案例0:
fragment=new MapsActivity();//此行显示错误
打破
案例1:
//实施
打破
案例2:
//实施
打破
}
fragmentManager.beginTransaction()
.替换(R.id.容器,碎片)
.commit();

}
这是因为
导航抽屉需要使用片段,但是您使用了活动,因此导致了错误

解决方案是您需要扩展
片段
,您需要将
映射活动
更改为
映射片段
,如下所示:

public class MapsFragment extends Fragment {


    private MapView mapView;

    public static MapsFragment newInstance() {
        MapsFragment fragment = new MapsFragment();
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.mapview, container, false);
        // Gets the MapView from the XML layout and creates it

        MapsInitializer.initialize(getActivity());


        switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity())) {
            case ConnectionResult.SUCCESS:
                Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show();
                mapView = (MapView) v.findViewById(R.id.map);
                mapView.onCreate(savedInstanceState);
                // Gets to GoogleMap from the MapView and does initialization stuff
                if (mapView != null) {
                     mapView.getMapAsync(new OnMapReadyCallback() {
                        @Override
                        public void onMapReady(GoogleMap googleMap) {
                            googleMap.getUiSettings().setMyLocationButtonEnabled(true);
                            googleMap.setMyLocationEnabled(true);
                            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
                            googleMap.animateCamera(cameraUpdate);
                        }
                    });

                }
                break;
            case ConnectionResult.SERVICE_MISSING:
                Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
                break;
            case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
                Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show();
        }

        return v;
    }

    @Override
    public void onResume() {
        mapView.onResume();
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }
}
布局xml文件为:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

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

</RelativeLayout>
而不是

fragment = new MapsActivity(); //this line show error

DrawerLayout

中,这是因为
navigation drawer
需要使用片段,但是您使用了活动,因此导致了错误

解决方案是您需要扩展
片段
,您需要将
映射活动
更改为
映射片段
,如下所示:

public class MapsFragment extends Fragment {


    private MapView mapView;

    public static MapsFragment newInstance() {
        MapsFragment fragment = new MapsFragment();
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.mapview, container, false);
        // Gets the MapView from the XML layout and creates it

        MapsInitializer.initialize(getActivity());


        switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity())) {
            case ConnectionResult.SUCCESS:
                Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show();
                mapView = (MapView) v.findViewById(R.id.map);
                mapView.onCreate(savedInstanceState);
                // Gets to GoogleMap from the MapView and does initialization stuff
                if (mapView != null) {
                     mapView.getMapAsync(new OnMapReadyCallback() {
                        @Override
                        public void onMapReady(GoogleMap googleMap) {
                            googleMap.getUiSettings().setMyLocationButtonEnabled(true);
                            googleMap.setMyLocationEnabled(true);
                            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
                            googleMap.animateCamera(cameraUpdate);
                        }
                    });

                }
                break;
            case ConnectionResult.SERVICE_MISSING:
                Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
                break;
            case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
                Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show();
        }

        return v;
    }

    @Override
    public void onResume() {
        mapView.onResume();
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }
}
布局xml文件为:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

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

</RelativeLayout>
而不是

fragment = new MapsActivity(); //this line show error

在您的
抽屉布局中

这对您有帮助吗?如果是,请接受。如果没有,请告诉我,谢谢帮助我的人,谢谢你,但是它给了我一个我不理解的错误,我现在上传一张图片。现在显示这个错误。。。您只需将您的
getfragmentmanage()
更改为
getSupportFragmentManage()
这对您有帮助吗?如果是,请接受。如果没有,请告诉我,谢谢帮助我的人,谢谢你,但是它给了我一个我不理解的错误,我现在上传一张图片。现在显示这个错误。。。您只需将您的
getfragmentmanage()
更改为
getSupportFragmentManage()