【发布时间】:2014-07-31 05:11:12
【问题描述】:
我知道使用嵌套片段存在问题。但是我的应用程序被设计为在片段上运行,如果我将使用地图活动,我的投射功能将会出错。
我想就如何实现这一目标向您寻求帮助。我一直在互联网上搜索,但找不到最佳解决方案。
我试过这段代码:
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) myFragmentActivity.getSupportFragmentManager().findFragmentById(R.id.map_con))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setMyLocationEnabled(true);
}
}
}
这会给我重复的错误,因为 R.id.map_con 是我的片段中的片段。
所以我寻找解决方法,这一次,R.id.map_con 是一个框架布局,在运行时我为它创建了 SupportMapFragment。
SupportMapFragment mSupportMapFragment = new SupportMapFragment();
myFragmentActivity.getSupportFragmentManager().beginTransaction()
.replace(R.id.map_con, mSupportMapFragment).commit();
虽然这不会在每次关闭和打开片段时给我一个副本。但我的错误是 mSupportMapFragment.getMap 始终为空。我不明白为什么它为空?。
mMap = mSupportMapFragment.newInstance().getMap();
if (mMap != null){
Log.e("ReportFragment","mMap is not empty");
}else{
Log.e("ReportFragment","mMap is empty");
}
非常感谢你们提供的任何意见,或者您是否还有其他解决方法但仍在此过程中,即片段内的片段
谢谢
chkm8
【问题讨论】:
标签: android google-maps supportmapfragment