【问题标题】:mClustermanager.additem() google maps throwing null pointer exception androidmClustermanager.additem()谷歌地图抛出空指针异常android
【发布时间】:2016-08-09 10:50:26
【问题描述】:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    MyItem myItem;
    private ClusterManager<MyItem> mClusterManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);


    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;






        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(31.530136,74.353225), 10));

        // Initialize the manager with the context and the map.
        // (Activity extends context, so we can pass 'this' in the constructor.)
        mClusterManager = new ClusterManager<MyItem>(this, mMap);

        // Point the map's listeners at the listeners implemented by the cluster
        // manager.
        mMap.setOnCameraChangeListener(mClusterManager);
        mMap.setOnMarkerClickListener(mClusterManager);

        // Add cluster items (markers) to the cluster manager.
       // addItems();
        myItem = new MyItem(31.530136,74.353225);
mClusterManager.addItem(myItem);
        myItem = new MyItem(31.529807, 74.352495);
        mClusterManager.addItem(myItem);
        myItem = new MyItem(31.530584, 74.353729);
        mClusterManager.addItem(myItem);
        myItem = new MyItem(31.531319, 74.351217);
        mClusterManager.addItem(myItem);






        // Add a marker in Sydney and move the camera
//        LatLng sydney = new LatLng(-34, 151);
//        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
//        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
    private void addItems() {

        // Set some lat/lng coordinates to start with.
        double lat = 31.531034;
        double lng = 74.352374;


        // Add ten cluster items in close proximity, for purposes of this example.
        for (int i = 0; i < 10; i++) {
            double offset = i / 60d;
            lat = lat + offset;
            lng = lng + offset;
            MyItem offsetItem = new MyItem(lat, lng);
            mClusterManager.addItem(offsetItem);
        }
    }


}

上面的类表示在 android 中谷歌地图提供的集群实用程序库中添加标记坐标的代码。但是当我运行代码时,它会在 mClustermanager.addItem() 给出错误,给出空指针异常。

【问题讨论】:

  • 您的mMap 似乎为空
  • 如果我只是使用 mMap 应用标记,它实际上可以工作,但使用 clustermanager 它会抛出空指针异常
  • 您是否为 MyItem 类实现了 ClusterItem?

标签: android google-maps markerclusterer


【解决方案1】:

我实际上在 gmaps-api-issues 中发现了几个问题(Issue 7696Issue 9021)。

但是,我不太确定您的案例是否与这些现有问题有关。

但是,您可以尝试给定的解决方法:

用户将其设备上的 Google Play 服务升级到 9.0.83 将解决此问题。您无需更新 SDK 版本。

有关更多详细信息,此 SO 帖子 - NullPointerException from Google maps 也可能有所帮助。

【讨论】:

    【解决方案2】:

    我通过从https://developers.google.com/maps/documentation/android-sdk/utility/setup 的演示应用程序复制 MyItem 类解决了这个问题(“获取 Maps SDK for Android 实用程序库”下的 zip 文件)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-21
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多