2016-08-15 3 views
0

Ich erstelle eine einfache Kartenanwendung mit der Google Maps-API. Wenn ich die App öffne, stürzt sie manchmal ab, weil der GoogleApiClient noch nicht verbunden ist. Ich habe ein paar Methoden, die ausgeführt werden und die API benötigen, um verbunden zu sein.Warten Sie, bis GoogleAPIClient geladen wird, bevor Sie versuchen, eine Verbindung herzustellen.

Wie verhindere ich die Abstürze, indem ich auf die Verbindung mit der API warte?

Hier einige meiner Code: onConnected:

@Override 
public void onConnected(Bundle connectionHint) 
{ 
    // this callback will be invoked when all specified services are connected 
    //Must ask for explicit permission 
    //ie: Opening settings action in order to change or give permissions 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
     currentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleClient); 
     mGoogleClient.connect(); 

     if (currentLocation != null) 
     { 
      currentLatLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()); 
      mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 15)); 
     } 

     else 
     { 
      if (mGoogleClient!=null) 
      { 
       mGoogleClient.connect(); 
      } 

      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleClient, locationRequest, this); 

     } 
    } 
} 

den aktuellen Standort Anfahrt:

private void getLocation() { 

    locationRequest = LocationRequest.create(); 
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    locationRequest.setInterval(60 * 1000); 
    locationRequest.setFastestInterval(30 * 1000); 

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

    if (mGoogleClient != null) 
    { 
     mGoogleClient.connect(); 
    } 


} 

Ich bin nicht sicher, was sonst noch benötigt wird. Bitte lassen Sie mich wissen, wenn ich mehr Code bereitstellen muss.

Edit: Crashlog

08-15 17:51:54.692 950-950/? E/AndroidRuntime: FATAL EXCEPTION: main 
              Process: com.hensh.fusedmap, PID: 950 
              java.lang.IllegalStateException: GoogleApiClient is not connected yet. 
               at com.google.android.gms.common.api.internal.zzh.zzb(Unknown Source) 
               at com.google.android.gms.common.api.internal.zzl.zzb(Unknown Source) 
               at com.google.android.gms.common.api.internal.zzj.zzb(Unknown Source) 
               at com.google.android.gms.location.internal.zzd.requestLocationUpdates(Unknown Source) 
               at com.hensh.fusedmap.MapsActivity.onConnected(MapsActivity.java:667) 
               at com.google.android.gms.common.internal.zzk.zzk(Unknown Source) 
               at com.google.android.gms.common.api.internal.zzj.zzi(Unknown Source) 
               at com.google.android.gms.common.api.internal.zzh.zzpx(Unknown Source) 
               at com.google.android.gms.common.api.internal.zzh.onConnected(Unknown Source) 
               at com.google.android.gms.common.api.internal.zzl.onConnected(Unknown Source) 
               at com.google.android.gms.common.api.internal.zzc.onConnected(Unknown Source) 
               at com.google.android.gms.common.internal.zzj$zzg.zzqL(Unknown Source) 
               at com.google.android.gms.common.internal.zzj$zza.zzc(Unknown Source) 
               at com.google.android.gms.common.internal.zzj$zza.zzw(Unknown Source) 
               at com.google.android.gms.common.internal.zzj$zzc.zzqN(Unknown Source) 
               at com.google.android.gms.common.internal.zzj$zzb.handleMessage(Unknown Source) 
               at android.os.Handler.dispatchMessage(Handler.java:102) 
               at android.os.Looper.loop(Looper.java:135) 
               at android.app.ActivityThread.main(ActivityThread.java:5294) 
               at java.lang.reflect.Method.invoke(Native Method) 
               at java.lang.reflect.Method.invoke(Method.java:372) 
               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 
+0

Post-Crash-Protokoll. –

+0

Ich vermute, dass deine Karte nicht bereit ist und zu einem Absturz führt, weil dein Code für den Standort in Ordnung ist. – PatrickZenker

+0

Bitte geben Sie an, wo es abstürzt. Warum rufen Sie connect 'mGoogleClient.connect();' in Ihrem onConnected auf? – tyczj

Antwort

2

Die GoogleAPIClient Bedürfnisse verbunden werden, bevor Sie versuchen, den Dienst zu nutzen. Also ist Ihr Anruf, es in onConnected zu verbinden, falsch. Wenn Sie in onConnected sind, bedeutet dies, dass der Client verbunden ist. Sie müssen die GoogleAPIClient in der onCreate Ihrer Aktivität bauen und sie idealerweise in der onStart Ihrer Activity verbinden.

private GoogleApiClient mGoogleApiClient; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.<your-xml>); 

    mGoogleApiClient = new GoogleApiClient.Builder(context) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
} 
public void onStart() { 
    super.onStart(); 
    mGoogleApiClient.connect(); 
} 

Darüber hinaus beim Aufbau der GoogleAPIClient können Sie einstellen, auch enableAutoManage() für die connect und disconnect automatisch verwaltet werden, so dass Sie nicht es manuell zu tun haben, wie here erläutert.

public class LocationActivity implements GoogleApiClient.OnConnectionFailedListener { 

    /** Google Services client */ 
    private GoogleApiClient mGoogleApiClient; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.payment_activity); 
     mGoogleApiClient = new GoogleApiClient.Builder(context) 
       .addConnectionCallbacks(this) 
       .enableAutoManage(this, this) 
       .addApi(LocationServices.API) 
       .build(); 
} 
0

Sie sollten die Snippet-Code platzieren mMap.animateCamera... etc in onMapLoaded.

Detect when Android v2 maps has loaded

Und entfernen mGoogleClient.connect(); in Ihrer onConnected Methode, weil Sie zweimal nicht anschließen müssen (oder mehr)

0

Es ist unklar, wie Sie Ihre Anwendung zu funktionieren. Wenn Sie Ihren Standort auf der Karte angezeigt werden sollen und aktualisiert, wenn Sie sich bewegen:

  1. Instantiate GoogleApiClient nur einmal in onCreate.
  2. Verbinden Sie GoogleApiClient in onResume.
  3. In onConnected, machen Sie Ihre Standortanfrage (n)
  4. In onLocationChanged, verschieben Sie Ihre Karte.

Es ist wahrscheinlich lohnt sich zu überprüfen: Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet

Verwandte Themen