我想从一个SupportMapFragment的地图,但它返回null。从我读这可能是因为该片段尚未完全显示出来,因此没有地图存在?我试着用executePendingTransactions(),但没有成功,到目前为止修复它。
任何想法如何解决这个问题?
下面是code
私人GoogleMap的地图;
私人SupportMapFragment mapFragment;
@覆盖
公共无效的onCreate(包savedInstanceState){
// ...
super.onCreate(savedInstanceState);
的setContentView(R.layout.screen_mission2);
GoogleMapOptions mapOptions =新GoogleMapOptions();
mapOptions.mapType(GoogleMap.MAP_TYPE_NORMAL)
.compassEnabled(真)
.rotateGesturesEnabled(假)
.tiltGesturesEnabled(假);
android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentManager.enableDebugLogging(真正的);
mapFragment = SupportMapFragment.newInstance(mapOptions);
FragmentTransaction fragmentTransaction = myFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.mapFragment,mapFragment);
fragmentTransaction.commit();
myFragmentManager.executePendingTransactions();
如果(mapFragment == NULL)Base.log(mapFragment ==空);
如果(图== NULL){
地图= mapFragment.getMap();
Base.log(地图应已初始化);
如果(图== NULL)Base.log(图还是空);
}
}
和布局文件:
<片段
机器人:ID =@ + ID / mapFragment
机器人:名称=com.google.android.gms.maps.SupportMapFragment
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
它返回以下日志
V / FragmentManager(24224):地址:SupportMapFragment {4078c4b8 ID = 0x7f06003d}
V / FragmentManager(24224):分配的片段指数SupportMapFragment {4078c4b8#1 ID = 0x7f06003d}
V / FragmentManager(24224):通过MoveTo创建:SupportMapFragment {4078c4b8#1 ID = 0x7f06003d}
D / EMR(24224):图应该已经初始化
D / EMR(24224):图仍空
解决方案
尝试将所有code引用您的 GoogleMap的
到 ONSTART( )
或 onResume()
。地图中的片段,该地图是没有实例化后才片段已经通过 onCreateView
走了(link),这恰好经过父活动已经走过的onCreate()
。另外,你需要检查你的 GoogleMap的
为空不管,因为如果谷歌播放服务没有安装,或者在地图无法使用某些其他原因,这将是空。
I'm trying to get a map from a SupportMapFragment but it returns null. From what I read this could be because the fragment is not yet fully displayed and therefore no map exists?! I tried fixing it using executePendingTransactions() but with no success so far.
Any ideas how to fix it?
Here is the code
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");
}
}
And the layout file:
<fragment
android:id="@+id/mapFragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
It returns the following log
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
解决方案
Try moving all code that references your GoogleMap
to onStart()
or onResume()
. The map in a map fragment isn't instantiated until after the fragment has gone through onCreateView
(link), which happens after the parent activity has gone through onCreate()
. Also, you need to check your GoogleMap
for null
regardless, because if google play services aren't installed, or the map isn't available for some other reason, it will be null
.
相关推荐
最新文章