【问题标题】:MapView crash without log没有日志的 MapView 崩溃
【发布时间】: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.

标签: android android-mapview


【解决方案1】:

显然您正在尝试使用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);

【讨论】:

  • 有什么方法可以使用 MapView 吗?显然我应该能够做到这一点:“MapView,Android View 类的子类,允许您在 Android View 中放置地图。View 代表屏幕的矩形区域,是 Android 应用程序的基本构建块和小部件。很像 MapFragment,MapView 充当地图的容器,通过 GoogleMap 对象公开核心地图功能。”
  • Fragment比较简单,不如试试看能不能满足你的需求。
  • 我试过了,但 SupportMapFragment 也出现同样的错误(应用重新启动,没有崩溃日志)
  • 使用片段用法更新您的问题。
  • 问题已更新。在问题的最后,我还描述了一种可能有用的行为。
猜你喜欢
  • 2018-04-03
  • 2016-03-13
  • 2020-06-10
  • 2012-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-18
相关资源
最近更新 更多