このonCreate
方法では、を利用しSupportMapFragment
て地図を表示しています。
SupportMapFragment fragment = new SupportMapFragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, fragment).commit();
これに伴い、マーカーを追加したいと思います。問題は、への呼び出しgetMap
がnullの場合、いつ再試行できますか?登録できるイベントはありますか、それとも私のアプローチ自体が間違っていますか?
mMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();
if(mMap == null)
//what do I do here?
地図は実際には電話に表示されていますが、マーカーを追加するための参照を取得するのに運がないようです。
アップデート:
コンストラクターを介してviaを作成した理由SupportMapFragment
は、標準setContentView
がクラッシュして機能しなかったためです。onCreate
これにより、実際にその時点で作成していたため、メソッドで参照を取得できなかったという苦境に陥りましSupportMapFragment
た。さらに調査したところ、私のsetContentView
問題は、プロジェクト全体の一部としてGoogle-play-servicesjarとmodule/srcの両方が設定されていないことの副産物であるようです。これらの両方を実行すると、現在は機能しており、期待どおりにsetContentView
参照を取得できます。getMap()
lots.xml..。
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
LotsActivity.java..。
public class LotsActivity extends FragmentActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lots);
GoogleMap mMap;
mMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();
if(mMap == null)
//this should not occur now
}