2016-03-28 25 views
-3

http://blog.teamtreehouse.com/beginners-guide-location-android以下教程中的错误

我按照这个教程,似乎在某处犯了一个错误。我不那么肯定,我出了问题,我只知道它说这是在那里我做了根据地图

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, LocationListener { 
private GoogleMap mMap; 
private GoogleApiClient mGoogleApiClient; 
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 
private LocationRequest mLocationRequest; 
public static final String TAG = MapsActivity.class.getSimpleName(); 


@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); 

    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 

    // Create the LocationRequest object 
    mLocationRequest = LocationRequest.create() 
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
      .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
      .setFastestInterval(1 * 1000); // 1 second, in milliseconds 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    setUpMap(); 
    mGoogleApiClient.connect(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    if (mGoogleApiClient.isConnected()) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
     mGoogleApiClient.disconnect(); 
    } 
} 


/** 
* 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; 

    // 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)); 
} 

@Override 
public void onConnected(Bundle bundle) { 
    Log.i(TAG, "Location services connected."); 
    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    if (location == null) { 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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; 
     } 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } 
    else { 
     handleNewLocation(location); 
    } 
} 

@Override 
public void onConnectionSuspended(int i) { 
    Log.i(TAG, "Location services suspended. Please reconnect."); 
} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    if (connectionResult.hasResolution()) { 
     try { 
      // Start an Activity that tries to resolve the error 
      connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST); 
     } catch (IntentSender.SendIntentException e) { 
      e.printStackTrace(); 
     } 
    } else { 
     Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode()); 
    } 

} 
private void handleNewLocation(Location location) { 
    Log.d(TAG, location.toString()); 
    double currentLatitude = location.getLatitude(); 
    double currentLongitude = location.getLongitude(); 
    LatLng latLng = new LatLng(currentLatitude, currentLongitude); 

    MarkerOptions options = new MarkerOptions() 
      .position(latLng) 
      .title("I am here!"); 

    mMap.addMarker(options); 

    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
} 

@Override 
public void onLocationChanged(Location location) { 
    handleNewLocation(location); 
} 

private void setUpMap() { 
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
} 

}

部分造成它说,它在造成控制台onResume()方法中的setUpMap();行和setUpMap方法。当我尝试运行应用程序时会发生什么情况,导致它立即关闭。我也意识到我把它从setUpMapIfNeeded更改为SetUpMap,因为在教程中我没有找到任何setUpMapIfNeeded()方法。

+1

添加logcat输出...意思是“添加错误请”。 – Shark

回答

1

你可以调用避免从onResumesetUpMap();或MMAP检查是不是在异步方式空

private void setUpMap() { 
    if (mMap != null) { 
     mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
    } 
} 

顾名思义,getMapAsync电话onMapReady,你没有任何保证onMapReady是之前已经调用onResume被称为