2017-09-14 4 views
1

Ich verweise Google Places photo android api. Ich verwende den folgenden Code in onBindViewHolder von RecyclerView Adapter. Die Hälfte der Zeit es Illegal State Ausnahme ausgelöst. Bitte helfen Sie.illegalStateException beim Abrufen eines Fotos von GeoDataClient.getPlacePhotos()

final Task<PlacePhotoMetadataResponse> photoMetadataResponse = mGeoDataClient.getPlacePhotos(placeId); 
     photoMetadataResponse.addOnCompleteListener(new OnCompleteListener<PlacePhotoMetadataResponse>() { 
      @Override 
      public void onComplete(@NonNull Task<PlacePhotoMetadataResponse> task) { 
       // Get the list of photos. 
       PlacePhotoMetadataResponse photos = task.getResult(); 
       // Get the PlacePhotoMetadataBuffer (metadata for all of the photos). 
       PlacePhotoMetadataBuffer photoMetadataBuffer = photos.getPhotoMetadata(); 
       // Get the first photo in the list. 
       PlacePhotoMetadata photoMetadata = photoMetadataBuffer.get(0); 
       // Get the attribution text. 
       CharSequence attribution = photoMetadata.getAttributions(); 
       // Get a full-size bitmap for the photo. 
       Task<PlacePhotoResponse> photoResponse = mGeoDataClient.getPhoto(photoMetadata); 
       photoResponse.addOnCompleteListener(new OnCompleteListener<PlacePhotoResponse>() { 
        @Override 
        public void onComplete(@NonNull Task<PlacePhotoResponse> task) { 
         PlacePhotoResponse photo = task.getResult(); 
         Bitmap bitmap = photo.getBitmap(); 
         ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
         bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
         Glide.with(mContext) 
           .load(stream.toByteArray()) 
           .asBitmap() 
           .error(R.drawable.cast_album_art_placeholder) 
           .centerCrop() 
           .thumbnail(.2f) 
           .into(holder.placeImage); 

        } 
       }); 
      } 
     }); 

Stacktrace:

E/UncaughtException: java.lang.IllegalStateException 
                   at com.google.android.gms.common.internal.zzbp.zzbg(Unknown Source) 
                   at com.google.android.gms.common.data.zzc.zzbu(Unknown Source) 
                   at com.google.android.gms.common.data.zzc.<init>(Unknown Source) 
                   at com.google.android.gms.location.places.internal.zzav.<init>(Unknown Source) 
                   at com.google.android.gms.location.places.internal.zzar.<init>(Unknown Source) 
                   at com.google.android.gms.location.places.PlacePhotoMetadataBuffer.get(Unknown Source) 

Antwort

1

Ich bin ziemlich sicher, dass das Problem ist, dass Ihre Anwendung aufgrund der Tatsache, abstürzt, dass Sie ein Foto von einem Ort abzurufen versuchen, die kein Foto hat anzeigen. Sie müssen einen Null-Check durchführen, bevor Sie versuchen, das erste Foto in Ihrem photoMetadataBuffer.get(0) abzurufen. Dies ist ein gutes Beispiel dafür, wie die Dokumentation von Google etwas unvollständig ist. Sie sollten so etwas wie die folgenden haben:

// Get the first photo in the list. 
if (photoMetadataBuffer != null) { 
    PlacePhotoMetadata photoMetadata = photoMetadataBuffer.get(0); 
// continue with your code 
} 

Wenn die photoMetadataBuffer null ist, dann gibt es kein Foto angezeigt werden und Sie können Ihre Anwendungslogik in geeigneter Weise, wie das Laden eines Standardbild behandeln, Feedback zu geben zu der Benutzer oder nicht die ImageView anzeigen.

Verwandte Themen