2016-11-05 130 views
0

這是我的代碼。在此googleMap1.setMyLocationEnabled(true);給出空指針異常。請給我建議。googleMap1.setMyLocationEnabled(true)not Working

公共類GooglePlacesActivity擴展FragmentActivity實現LocationListener的{

private static final String GOOGLE_API_KEY = "******************************"; 
GoogleMap googleMap1; 
EditText placeText; 
double latitude = 0; 
double longitude = 0; 
private int PROXIMITY_RADIUS = 5000; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    //show error dialog if GoolglePlayServices not available 
    if (!isGooglePlayServicesAvailable()) { 
     finish(); 
    } 
    setContentView(R.layout.activity_google_places); 

    placeText = (EditText) findViewById(R.id.placeText); 
    Button btnFind = (Button) findViewById(R.id.btnFind); 
    SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap); 
    googleMap1 = fragment.getMap(); 
    googleMap1.setMyLocationEnabled(true); 


    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    String bestProvider = locationManager.getBestProvider(criteria, true); 
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    Location location = locationManager.getLastKnownLocation(bestProvider); 
    if (location != null) { 
     onLocationChanged(location); 
    } 
    locationManager.requestLocationUpdates(bestProvider, 20000, 0, this); 

    btnFind.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String type = placeText.getText().toString(); 
      StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?"); 
      googlePlacesUrl.append("location=" + latitude + "," + longitude); 
      googlePlacesUrl.append("&radius=" + PROXIMITY_RADIUS); 
      googlePlacesUrl.append("&types=" + type); 
      googlePlacesUrl.append("&sensor=true"); 
      googlePlacesUrl.append("&key=" + GOOGLE_API_KEY); 

      GooglePlacesReadTask googlePlacesReadTask = new GooglePlacesReadTask(); 
      Object[] toPass = new Object[2]; 
      toPass[0] = googleMap1; 
      toPass[1] = googlePlacesUrl.toString(); 
      googlePlacesReadTask.execute(toPass); 
     } 
    }); 
} 

private boolean isGooglePlayServicesAvailable() { 
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
    if (ConnectionResult.SUCCESS == status) { 
     return true; 
    } else { 
     GooglePlayServicesUtil.getErrorDialog(status, this, 0).show(); 
     return false; 
    } 
} 


@Override 
public void onLocationChanged(Location location) { 
    latitude = location.getLatitude(); 
    longitude = location.getLongitude(); 
    LatLng latLng = new LatLng(latitude, longitude); 
    googleMap1.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    googleMap1.animateCamera(CameraUpdateFactory.zoomTo(12)); 
} 

@Override 
public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub 
} 

}`

回答

1

你必須使用getMapAsync(this)象下面這樣:

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 

,並覆蓋onMapReady

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 
}