2016-08-09 64 views
0
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); 
     } 
    } 


} 

上面的類表示在谷歌地圖提供的集羣實用程序庫中添加標記座標的代碼。但是當我運行代碼時,會在mClustermanager.addItem()處給出錯誤,從而給出空指針異常。mClustermanager.additem()谷歌地圖拋出空指針異常android

+0

它似乎你的'mMap'爲空 –

+0

如果我只是使用mMap應用標記它實際上工作,但與clustermanager它拋出空指針異常 –

+0

你實現了MyItem類的ClusterItem? –

回答

0

我居然發現一對夫婦的登錄gmaps-API的問題的問題(Issue 7696Issue 9021)。

但是,我不確定您的情況是否與某些現有問題有關。

但是,您可以嘗試給出的解決方法:

用戶升級谷歌Play服務,其設備上的9.0.83將解決這個問題。您不需要更新SDK版本。

爲了增加更多的詳細信息,這個SO後 - NullPointerException from Google maps也可能有所幫助。