2017-09-14 4 views
1

私はGoogle Places photo android apiを参照しています。 RecyclerView AdapterのonBindViewHolderで以下のコードを使用しています。 半分の時間で、不正な状態の例外がスローされます。助けてください。GeoDataClient.getPlacePhotos()から写真を取得中にillegalStateException

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) 

答えて

1

私が原因あなたは写真を持っていない場所から写真を取得しようとしているという事実のために、問題は、アプリケーションがクラッシュしていることであることをかなり確信しています表示する。 photoMetadataBuffer.get(0)の最初の写真を取得する前に、ヌルチェックを行う必要があります。これは、Googleのドキュメントが、提供されているサンプルコードからいくらか不完全である方法の良い例です。 photoMetadataBufferがnullの場合、このようなにフィードバックを与え、デフォルトの画像を読み込むと、その後、表示される写真はありません、あなたは適切なアプリケーションロジックを処理することができ、

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

:あなたは、次のようなものを持っている必要がありますユーザー、またはImageViewを表示しない。

関連する問題