Android studio 我有一个映射片段活动,我想将其转换为片段

Android studio 我有一个映射片段活动,我想将其转换为片段,android-studio,Android Studio,我是android开发的新手,我已经开发了一个应用程序,现在我正在尝试将活动转换为fragnment。我不明白如何将现有代码更改为片段 Maps.java: package com.ambisave; import android.Manifest; import android.content.pm.PackageManager; import android.os.Bundle; import android.support.v4.app.Fragment; imp

我是android开发的新手,我已经开发了一个应用程序,现在我正在尝试将活动转换为fragnment。我不明白如何将现有代码更改为片段

Maps.java:

  package com.ambisave;

  import android.Manifest;
  import android.content.pm.PackageManager;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.RelativeLayout;

  import com.google.android.gms.maps.CameraUpdateFactory;
  import com.google.android.gms.maps.GoogleMap;
  import com.google.android.gms.maps.MapFragment;
  import com.google.android.gms.maps.OnMapReadyCallback;
  import com.google.android.gms.maps.SupportMapFragment;
  import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  import com.google.android.gms.maps.model.CameraPosition;
  import com.google.android.gms.maps.model.LatLng;
  import com.google.android.gms.maps.model.MarkerOptions;

   public class Maps extends Fragment implements OnMapReadyCallback {

   private GoogleMap mMap;

    @Override
    public void onMapReady(GoogleMap map) {
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    CameraPosition googlePlex = CameraPosition.builder()
            .target(new LatLng(37.4219999, -122.0862462))
            .zoom(16)
            .bearing(0)
            .tilt(45)
            .build();

    map.moveCamera(CameraUpdateFactory.newCameraPosition(googlePlex));
    map.addMarker(new MarkerOptions()
            .position(new LatLng(17.440466, 78.496668))
            .title("SVIT")
            .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher)));


    if (checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    map.setMyLocationEnabled(true);


   }

   private int checkSelfPermission(Maps maps, String accessCoarseLocation) {


   }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View view = inflater.inflate(R.layout.fragment_maps, null);

     setContentView(R.layout.fragment_maps);
     MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    GoogleMap mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
     }
  }
fragment_maps.xml

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

  <!-- TODO: Update blank fragment layout -->
  <fragment
      android:name="com.google.android.gms.maps.SupportMapFragment"
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

 </RelativeLayout>
替换

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this); with


MapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this); with


MapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);