【发布时间】:2017-04-04 07:56:12
【问题描述】:
我有一个
public abstract class MyMapFragment implements OnMapReadyCallback
{
//
public GoogleMap googleMap;
SupportMapFragment mapFragment;
@IdRes
public abstract int getSupportMapFragId();
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// http://stackoverflow.com/a/36592000/5102206
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
// Do something for lollipop and above versions
mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(getSupportMapFragId());
} else {
// do something for phones running an SDK before lollipop
mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(getSupportMapFragId());
}
mapFragment.getMapAsync(this);
}
//..
@Override
public void onMapReady(GoogleMap map) {
this.googleMap = map;
}
}
根据我的断点onViewCreated()被调用,但是onMapReady()没有被调用(断点this.googleMap = map没有被触发)
到目前为止,在 Android 5、6 和 7 上运行良好,我可以看到地图.. 在 Android 4.X (API 16 - API 19) 设备上,我的应用程序启动,但它似乎在那里冻结...我看到一个白色的空白屏幕。
在 Android 4.X 操作系统设备上: 1.使用getFragmentManager(),mapFragment对象在else条件后为null。 2. 使用 getChildFragmentMenager() 时,mapfragment 似乎有效且非空,但未触发 onMapReady。
我在这里错过了什么?
【问题讨论】:
-
为什么不使用extend
SupportMapFragment或者直接使用呢? Android Jelly(即 API 16)及更高版本涵盖了 95.2% 的 Google Play 支持设备。 -
@JPVentura 我暂时这样做了,但这并没有改变。我得到了同样的结果。
-
1.为什么要将
SupportMapFragment嵌套在MyMapFragment中? 2. 为什么要明确检查没有人使用的 API 级别? -
我也有同样的问题,是真的。
标签: google-maps-android-api-2 supportmapfragment