【发布时间】:2016-07-24 01:11:43
【问题描述】:
我有一个只有 MapView 元素的简单活动。地图加载正常,但只要我放大/缩小应用程序就会崩溃而没有给我任何类型的日志(我也尝试过不使用包过滤器,但出现了任何有用的东西)。我唯一可以怀疑的是内存问题,但我不知道如何检查并最终解决它。
活动:
public class PointsOfInterest extends AppCompatActivity implements OnMapReadyCallback {
private MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_map);
/* MapView */
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onMapReady(GoogleMap map) {
map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
@Override
protected void onPause() {
mapView.onPause();
super.onPause();
}
@Override
protected void onDestroy() {
mapView.onDestroy();
super.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="230dp" />
</LinearLayout>
更新:片段同样的问题:
活动:
public class PointsOfInterest extends AppCompatActivity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_map);
SupportMapFragment mMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
}
}
布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="230dp" />
</LinearLayout>
顺便说一句,如果我尝试使用“match_parent”而不是 230dp,只要我移动地图或放大/缩小应用程序就会重新启动(换句话说,当我必须加载新的地图数据时)。地图越小,我可以使用的时间越长。 这让我更加思考内存问题(无论如何,RAM 使用量只有 40MB,加载地图时峰值为 50MB,立即 GC 再次下降到 40MB)
【问题讨论】:
-
当您查看 logcat 时,您是否将其设置为显示所有内容而不仅仅是正在运行的应用程序?
-
在 Android Studio 中,我尝试了“仅显示选定的应用程序”和“无过滤器”选项。我也尝试使用 adb (logcat -d > log.txt) 检查 logcat,这似乎是唯一与崩溃有关的事情:I/WindowState(760): WIN DEATH: Window{74b2951 u0 it.techdt .mycars/it.techdt.mycars.PointsOfInterest.PointsOfInterest} I/ActivityManager(760):处理 it.techdt.mycars(pid 32400)已死 W/ActivityManager(760):强制删除 ActivityRecord{7164b63 u0 it.techdt.mycars /.PointsOfInterest.PointsOfInterest t20622}:应用程序死机,没有保存状态
-
你为什么打电话给
mapView.onCreate(或任何其他生命周期事件)?你不应该那样做。 -
Google 文档和示例明确告知管理 MapView 生命周期(并且 mapView.onCreate 用于显示地图,否则不会加载和显示):developers.google.com/maps/documentation/android-api/…
-
这很奇怪。顺便说一句,你在链接的文档上看到过这个吗:
Users viewing the map cannot zoom or pan the map.