?
现象repro:
1. 第一次运行App,没有问题。
2. 按返回键推出App。
3. 再次运行App,出现如下错误,App无法启动。
4. 再次运行App,App正常启动。
5. 如此往复。
?
原因:
这个错误发生在App启动,初始化百度地图时。通常是由上次退出App时百度地图的后台Service资源释放不完全造成的。
如果该错误出现在退出App时,则是百度地图相关模块的Destory顺序有问题。
?
正确初始化/释放时序:
class="java" name="code"> private BMapManager mBMapManager; private MapView mMapView; private MKOfflineMap mapOffline; private MKSearch mapSearch; private Context context; @Override protected void onCreate(Bundle savedInstanceState) { Log.i(Name, "onCreate"); super.onCreate(savedInstanceState); this.context = this.getApplicationContext(); mBMapManager = new BMapManager(getApplication()); mBMapManager.init("your key", null); /*** * 必须在初始化百度地图manager之后才能setContentView * 在官方文档中也有相同描述 ***/ setContentView(R.layout.activity_main); GeoPoint yourLocation = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6)); initBaiduMap(yourLocation); } @Override protected void onDestroy() { Log.i(Name, "onDestroy"); /*** * 必须按此顺序执行destory! * 有使用离线地图的离线地图控制器也要销毁 ***/ mapOffline.destroy(); mMapView.destroy(); mBMapManager.destroy(); super.onDestroy(); } @Override protected void onPause() { Log.i(Name, "onPause"); mMapView.onPause(); mBMapManager.stop(); super.onPause(); } @Override protected void onResume() { Log.i(Name, "onResume"); mMapView.onResume(); mBMapManager.start(); super.onResume(); } private void initBaiduMap(GeoPoint point) { mMapView = (MapView) findViewById(R.id.bmapsView); mMapView.setBuiltInZoomControls(true); MapController mMapController = mMapView.getController(); mapOffline = new MKOfflineMap(); mapOffline.init(mMapController, new MKOfflineMapListener() { @Override public void onGetOfflineMapState(int type, int state) { switch (type) { case MKOfflineMap.TYPE_DOWNLOAD_UPDATE: MKOLUpdateElement update = mapOffline.getUpdateInfo(state); break; case MKOfflineMap.TYPE_NEW_OFFLINE: Log.d("OfflineDemo", String.format("add offlinemap num:%d", state)); break; case MKOfflineMap.TYPE_VER_UPDATE: Log.d("OfflineDemo", String.format("new offlinemap ver")); break; } } }); int num = mapOffline.scan(); System.out.println(">>>offlineMaps: " + num); mMapController.setCenter(point); mMapController.setZoom(12); mapSearch = new MKSearch(); mapSearch.init(mBMapManager, new BaiduMapSearchListener(this, mMapView)); }
?