2016-12-26 37 views
3

我想在我的一個視圖中集成地圖視圖。android studio onMapReady不叫

我已經生成了一個新的地圖片段。它以不同的視角出現,像魅力一樣起作用。

然後,我嘗試將代碼集成到一個正常的活動中(一個帶有操作欄等等)。它有點作品 - 出現在屏幕上,但是MapReady在那個環境中永遠不會被調用(換句話說,我停留在非洲而不是去悉尼)。

一直在好幾個小時......

下面是XML代碼:

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

</android.support.design.widget.AppBarLayout> 

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:map="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/map" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.jp.artfoundui.MapsActivity" /> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/monument_capture" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|center" 
    android:layout_margin="@dimen/fab_margin" 
    app:srcCompat="@android:drawable/ic_menu_camera" /> 

這裏是Java代碼:

public class MonumentActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_monument); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap mMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(MapsActivity.this); 

    } 
     /** 
    * Manipulates the map once available. 
    * This callback is triggered when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Sydney, Australia. 
    * If Google Play services is not installed on the device, the user will be prompted to install 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     // Add a marker in Sydney and move the camera 
     LatLng sydney = new LatLng(-34, 151); 
     mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    } 
} 
} 

有什麼建議嗎?

+0

我知道這聽起來愚蠢,但你真的使用調試器,看看它是否步到onMapReady()?我正在查看我的一些個人代碼,我們的工作基本匹配。也許在MapsReady裏面有什麼不起作用? –

+0

不錯的建議。我查了一下,沒有打電話給onMapReady。 – jpberry

+0

但是當我使用調試器時,我注意到onCreate也沒有被調用。自然,onMapReady永遠不會被註冊。 所以,現在,爲什麼不onCreate被調用。我檢查了另一個演示代碼(在屏幕上截取片段的標準示例),並且onCreate確實被調用。但是,一旦我把片段放在我的活動中(上面的代碼),onCreate就不會被調用... – jpberry

回答

2

我得到了答案 - 有點。

有關片段活動的整個代碼從未被調用。我嘗試了幾種解決方案來激活這個片段,但是都已經到了死衚衕......

另一種解決方案是繼續使用主要活動(AppCompatActivity的一個子類)。事實證明,AppCompatActivity可以直接實現一個OnMapReadyCallback。

所以我添加了「實現OnMapReadyCallback」,然後直接添加了「onMapReady」回調。代碼如下所示:

public class MonumentActivity extends AppCompatActivity implements OnMapReadyCallback { 

private GoogleMap mMap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_monument); 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.monument_map); 
    mapFragment.getMapAsync(this); 
[...] 
@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 

    // Add a marker in Sydney and move the camera 
    LatLng louvres = new LatLng(48.860294, 2.338629); 
    mMap.addMarker(new MarkerOptions().position(louvres).title("Marker sur le Louvres")); 
    // mMap.moveCamera(CameraUpdateFactory.newLatLng(louvres)); 
    // mMap.moveCamera(CameraUpdateFactory.zoomTo(18)); 
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(louvres, 15)); 
} 

工作正常。我得到一個完整的活動(與工具欄,菜單活動,並有一個額外的浮動按鈕),底層的地圖工作正常...

+0

仍然沒有調用 –