【发布时间】:2013-01-22 10:28:08
【问题描述】:
我对嵌入新 Google Maps API 中的 SuportMapFragment 的 Fragment 有疑问。当我的片段被创建时,它会在AsyncTask 中获取一些数据,从onResume 方法开始。发生这种情况时,MapFragment 保持在屏幕外,而是显示一个进度条。
当AsyncTask完成后,我注册了mapView的onGlobalLayout事件。然后我显示片段,这导致地图视图被显示和布局。 onGlobalLayout 被触发,动画开始。
这在相当长的一段时间内运行良好,直到我开始优化数据收集AsyncTask 中的操作以几乎立即完成。现在应用程序在启动时经常会崩溃,在 Release 中比在 Debug 中更常见,这就是为什么我认为我正在处理我不知道的赛车情况。
异常如下:
地图大小不应为 0。很可能,地图视图的布局尚未发生。
非常感谢任何见解。
供参考:这是似乎导致崩溃的代码:
// Hide status UI
this.progressBar.setVisibility(View.INVISIBLE);
this.statusTextView.setVisibility(View.INVISIBLE);
// Update state
this.uraDataDownloadFinished = true;
// Schedule map translation to occur when layout is complete
final View mapView = this.mapFragment.getView();
if (mapView.getViewTreeObserver().isAlive())
{
mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@SuppressWarnings("deprecation")
@SuppressLint("NewApi") // We check which build version we are using.
@Override
public void onGlobalLayout()
{
Log.d(TAG, "onGlobalLayout()");
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
{
mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
else
{
mapView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
// here we crash
this.map.animateCamera(
CameraUpdateFactory.newLatLngBounds(
LatLngBounds.builder()
.include(topLeft)
.include(topRight)
.include(bottomLeft)
.include(bottomRight)
.build(),
0),
durationMs,
null);
}
});
}
// Show map (which will eventually trigger onGlobalLayout)
this.getFragmentManager().beginTransaction().show(this.mapFragment).commit();
this.mapFragment.getMap().setOnCameraChangeListener(this);
非常感谢!
【问题讨论】:
标签: android google-maps-android-api-2