2016-08-15 56 views
0

我正在使用Google Maps API構建一個簡單的地圖應用程序。當我打開應用程序時,它有時會崩潰,因爲GoogleApiClient尚未連接。我有幾個運行的方法需要連接API。在嘗試連接之前先等待GoogleAPIClient加載

如何通過等待API連接來防止崩潰?

下面是我的一些代碼: 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); 

     } 
    } 
} 

獲取當前位置:

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


} 

我不知道需要什麼。請讓我知道是否需要提供更多代碼。

編輯:崩潰日誌

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 log。 –

+0

我猜你的地圖沒有準備好,導致崩潰,因爲你的地址代碼沒問題。 – PatrickZenker

+0

請指明它崩潰的地方。你爲什麼要在你的onConnected中調用connect'mGoogleClient.connect();'? – tyczj

回答

2

的需求GoogleAPIClient連接您嘗試使用服務之前。所以你撥打電話onConnected是錯誤的。如果您在onConnected這意味着連接的客戶端。您需要在您的活動的onCreate中構建GoogleAPIClient,並且理想情況下將其連接到ActivityonStart

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

此外,建設時GoogleAPIClient還可以設置enableAutoManage()connectdisconnect進行自動管理,使您不必做手工,作爲解釋here

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(); 
} 
相關問題