2016-12-26 21 views
3

自分のビューの1つにマップビューを統合したい。android studio onMapReadyが呼び出されていない

私は新しいマップフラグメントを生成しました。それは別の視点で登場し、魅力のように機能します。

次に、コードを通常のアクティビティ(アクションバーなどのコード)に統合しようとしました。ちょっとした作品 - 画面上に表示されますが、onMapReadyはその環境で呼び出されることはありません(言い換えれば、私はシドニーに行くのではなく、アフリカで立ち往生しています)。時間のためにそれにされて

...ここ

はXMLコードです:

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)); 
    } 
} 
} 

<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コードであります

何か提案がありますか?

+0

私はこれはダムと思っていますが、実際にはデバッガを使用して、 onMapReady()に挿入しますか?私は私の個人的なコードのいくつかを見ており、私たちの仕事は基本的に一致しています。おそらくonMapReadyの中に何かがありますが、それは動作していませんか? –

+0

良い提案。私は、ええと、onMapReadyへの呼び出しをチェックしました。 – jpberry

+0

しかし、私がデバッガで遊んでいたときに、onCreateが呼び出されないことに気がつきました。当然、onMapReadyは決して登録されません。 これで、なぜonCreateが呼び出されないのですか?私は他のデモコード(画面上の断片をちょうど抜く標準的な例)をチェックし、onCreateは実際に呼び出されます。しかし、いったん私の活動(上記のコード)にフラグメントを配置すると、onCreateは呼び出されません。 – jpberry

答えて

2

私は答えを得ました。

フラグメントアクティビティに関するコード全体が呼び出されることはありません。私はこの断片を活性化するためにいくつかのソリューションを試しましたが、すべてが終わりに達しました...

代わりに、主なアクティビティ(AppCompatActivityのサブクラス)を使い続けることです。 AppCompatActivityは、OnMapReadyCallbackを直接実装することができます。

私は "implement 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

まだ呼び出されていません –

関連する問題