2015-10-07 45 views
0

所以我有一個谷歌地圖應用程序在android工作室,我想設置它。我看着樹屋他們如何設置它與谷歌自己的位置提供商和一切必要http://blog.teamtreehouse.com/beginners-guide-location-androidAndroid Studio模擬器崩潰LocationServices.fusedLocationApi

我已經做了一切它說,它在我的手機上工作正常,但模擬器崩潰在啓動時,它說故障是在該行

LocationServices.FusedLocationApi.requestLocationUpdates(this.mGoogleApiClient, this.mLocationRequest, this); 

我不知道我該怎麼辦。我甚至使用控制檯來geo修復模擬器中gps的位置,但沒有任何結果。這是我的完整代碼

public class Guide extends AppCompatActivity implements 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, 
    LocationListener 


private GoogleMap   mMap; // Might be null if Google Play services APK is not available. 
private GoogleApiClient  mGoogleApiClient; 
private LocationRequest  mLocationRequest; 
private LatLng    currentPos; 

private boolean    centerCamera; 
private boolean    isSatelliteChecked = false; 

public static final String TAG = Guide.class.getSimpleName(); 
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_guide); 
    this.centerCamera = true; 
    setUpMapIfNeeded(); 

    // Create the Google API Client 
    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(1000); // 1 second, in milliseconds 




} 

@Override 
public boolean onPrepareOptionsMenu(Menu menu) 
{ 
    MenuItem checkable = menu.findItem(R.id.map_type); 
    checkable.setChecked(isSatelliteChecked); 
    return true; 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) 
{// Inflate the menu; this adds items to the action bar if it is 
    // present. 
    getMenuInflater().inflate(R.menu.menu, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) 
{ 
    // Handle item selection 
    switch (item.getItemId()) { 
     case R.id.menu_settings: 

      return true; 
     case R.id.map_type: 
      isSatelliteChecked = !item.isChecked(); 
      item.setChecked(isSatelliteChecked); 
      if(isSatelliteChecked) 
      { 
       mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
      } 
      else 
      { 
       mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
      } 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

@Override 
protected void onResume() 
{ 
    super.onResume(); 
    setUpMapIfNeeded(); 
    mGoogleApiClient.connect(); 
    this.mMap.setMyLocationEnabled(true); 
    if (this.currentPos != null) 
    { 
     this.mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentPos, 16)); 
    } 
} 

@Override 
protected void onPause() 
{ 
    if (this.mGoogleApiClient.isConnected()) 
    { 
     LocationServices.FusedLocationApi.removeLocationUpdates(this.mGoogleApiClient, this); 
     this.mGoogleApiClient.disconnect(); 
    } 
    if (this.mMap!=null) 
    { 
     mMap.setMyLocationEnabled(false); 
     this.centerCamera = true; 
    } 
    super.onPause(); 

} 
/** 
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly 
* installed) and the map has not already been instantiated.. This will ensure that we only ever 
* call {@link #setUpMap()} once when {@link #mMap} is not null. 
* <p/> 
* If it isn't installed {@link SupportMapFragment} (and 
* {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to 
* install/update the Google Play services APK on their device. 
* <p/> 
* A user can return to this FragmentActivity after following the prompt and correctly 
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not 
* have been completely destroyed during this process (it is likely that it would only be 
* stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this 
* method in {@link #onResume()} to guarantee that it will be called. 
*/ 
private void setUpMapIfNeeded() 
{ 
    // Do a null check to confirm that we have not already instantiated the map. 
    if (this.mMap == null) 
    { 
     // Try to obtain the map from the SupportMapFragment. 
     this.mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 
     // Check if we were successful in obtaining the map. 
     if (this.mMap != null) 
     { 
      setUpMap(); 
     } 
    } 
} 

/** 
* This is where we can add markers or lines, add listeners or move the camera. In this case, we 
* just add a marker near Africa. 
* <p/> 
* This should only be called once and when we are sure that {@link #mMap} is not null. 
*/ 
private void setUpMap() { 
    this.mMap.setMyLocationEnabled(true); 
    this.mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 

} 


private void handleNewLocation(Location location) 
{ 
    Log.d(this.TAG, location.toString()); 
} 

@Override 
public void onConnected(Bundle bundle) 
{ 
    Log.i(this.TAG, "Location services connected."); 
    Location location = LocationServices.FusedLocationApi.getLastLocation(this.mGoogleApiClient); 
    if (location == null) 
    { 
     LocationServices.FusedLocationApi.requestLocationUpdates(this.mGoogleApiClient, this.mLocationRequest, this); 
    } 
    else 
    { 
     handleNewLocation(location); 
    } 
} 

@Override 
public void onConnectionSuspended(int i) 
{ 
    Log.i(this.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(this.TAG, "Location services connection failed with code " + connectionResult.getErrorCode()); 
    } 
} 

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

請添加日誌進行崩潰。 – Tauqir

回答

0

這解決了我的問題。

在gradle build中,我檢查兩個play-services都有相同的版本。這裏10.2.6

編譯 'com.google.android.gms:發揮服務,地圖:10.2.6' 編譯 'com.google.android.gms:發揮服務地點:10.2.6'