2016-08-18 34 views
1

我是新的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> 

這是我得到當我嘗試建立錯誤Gradle請幫助我。

Error:(80, 101) error: cannot find symbol method getMap() 
:app:compileDebugJavaWithJavac FAILED 
Error:Execution failed for task ':app:compileDebugJavaWithJavac'. 
> Compilation failed; see the compiler error output for details. 

回答

1

更換

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


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