向arrayList添加标记会导致应用崩溃

时间:2019-01-30 20:52:39

标签: java android

我正在开发基于地图的应用程序。该活动列出了很少的要求,点击请求将打开一个地图活动,其中将显示驾驶员和驾驶员之间的距离。单击请求应用程序崩溃后,但重复单击后将其重定向到地图活动。

当应用崩溃日志上显示 result_2 <- df2 %>% mutate(id = paste0(id, "_a")) %>% bind_rows(df1) %>% select(-id_df2) %>% replace(., is.na(.), 0) %>% group_by(id) %>% summarise(count1 = sum(count1), count2 = sum(count2), type = max(type)) %>% mutate(id_df2 = as.factor(id)) %>% select(c(id_df2, type, count1, count2), -id) 时:

nullPointerexception

代码如下:

markers.add(mMap.addMarker(new MarkerOptions().position(driverLocation).title("your location")));

}

1 个答案:

答案 0 :(得分:2)

由于地图异步,因此地图将在以后加载,因此mMap为空。

要解决此问题,请移动代码的这一部分:

RelativeLayout mapLayout = (RelativeLayout) findViewById(R.id.mapRelativeLayout);
    mapLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            LatLng driverLocation = new LatLng(intent.getDoubleExtra("driverLatitude", 0), intent.getDoubleExtra("driverLongitude", 0));
            LatLng requestLocation = new LatLng(intent.getDoubleExtra("requestLatitude",0), intent.getDoubleExtra("requestLongitude",0));

            markers.add(mMap.addMarker(new MarkerOptions().position(driverLocation).title("your location")));
            markers.add(mMap.addMarker(new MarkerOptions().position(requestLocation).title("driverLocation")));

            LatLngBounds.Builder builder = new LatLngBounds.Builder();
            for (Marker marker : markers) {
                builder.include(marker.getPosition());
            }
            LatLngBounds bounds = builder.build();

            int padding = 60; // offset from edges of the map in pixels
            CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);


            mMap.animateCamera(cu);
        }
    });

onMapReady之后,转到mMap = googleMap;方法。像这样:

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    RelativeLayout mapLayout = (RelativeLayout) findViewById(R.id.mapRelativeLayout);
    mapLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            LatLng driverLocation = new LatLng(intent.getDoubleExtra("driverLatitude", 0), intent.getDoubleExtra("driverLongitude", 0));
            LatLng requestLocation = new LatLng(intent.getDoubleExtra("requestLatitude",0), intent.getDoubleExtra("requestLongitude",0));

            markers.add(mMap.addMarker(new MarkerOptions().position(driverLocation).title("your location")));
            markers.add(mMap.addMarker(new MarkerOptions().position(requestLocation).title("driverLocation")));

            LatLngBounds.Builder builder = new LatLngBounds.Builder();
            for (Marker marker : markers) {
                builder.include(marker.getPosition());
            }
            LatLngBounds bounds = builder.build();

            int padding = 60; // offset from edges of the map in pixels
            CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);


            mMap.animateCamera(cu);
        }
    });

}