私はSupportMapFragmentからマップを取得しようとしていますが、nullを返します。フラグメントがまだ完全に表示されておらず、したがってマップが存在しないので、私が読んだことからそうである可能性があります。私はexecutePendingTransactions()を使用して修正しようとしましたが、これまでのところ成功しませんでした。mapFragment.getMap()がnullを返します
これを修正する方法はありますか?
ここコード
private GoogleMap map;
private SupportMapFragment mapFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
//...
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_mission2);
GoogleMapOptions mapOptions = new GoogleMapOptions();
mapOptions.mapType(GoogleMap.MAP_TYPE_NORMAL)
.compassEnabled(true)
.rotateGesturesEnabled(false)
.tiltGesturesEnabled(false);
android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentManager.enableDebugLogging(true);
mapFragment = SupportMapFragment.newInstance(mapOptions);
FragmentTransaction fragmentTransaction = myFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.mapFragment, mapFragment);
fragmentTransaction.commit();
myFragmentManager.executePendingTransactions();
if(mapFragment == null) Base.log("mapFragment==null");
if(map==null){
map = mapFragment.getMap();
Base.log("map should have been initialized");
if(map==null) Base.log("map still null");
}
}
とレイアウトファイルです。それは私がするので、これは可能性が読んだものから、次のログ
V/FragmentManager(24224): add: SupportMapFragment{4078c4b8 id=0x7f06003d}
V/FragmentManager(24224): Allocated fragment index SupportMapFragment{4078c4b8 #1 id=0x7f06003d}
V/FragmentManager(24224): moveto CREATED: SupportMapFragment{4078c4b8 #1 id=0x7f06003d}
D/EMR (24224): map should have been initialized
D/EMR (24224): map still null
に述べたようにフラグメントを操作するためにそれを使うのか? – yugidroid
はい私はそれを宣言しました(コード部分を編集しました、上記参照) – cos4
初期化されていますか?そうでなければ、何もない、すなわち「ヌル」と同じになる。 – yugidroid