没有日志的MapView崩溃

时间:2016-07-23 15:40:27

标签: android android-mapview

我只有一个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)

1 个答案:

答案 0 :(得分:0)

显然您正在尝试使用lite mode中的地图。该文档说,在该模式下,不允许用户平移或缩放。

如果您确实需要平移/缩放功能,请改为:

1)将此片段添加到您的活动布局xml:

...
<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="match_parent" />
...

2)在您的活动onCreate上初始化这样的地图:

mMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMapFragment.getMapAsync(this);
相关问题