From 98e65d066abb974c2f02229b1e944b26d839b83b Mon Sep 17 00:00:00 2001 From: Yaron Budowski Date: Thu, 18 Jan 2024 23:40:01 -0600 Subject: [PATCH] #1327 - crash fix --- .../android/ObservationViewerFragment.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/iNaturalist/src/main/java/org/inaturalist/android/ObservationViewerFragment.java b/iNaturalist/src/main/java/org/inaturalist/android/ObservationViewerFragment.java index ae7deec6..1e3fd5ef 100644 --- a/iNaturalist/src/main/java/org/inaturalist/android/ObservationViewerFragment.java +++ b/iNaturalist/src/main/java/org/inaturalist/android/ObservationViewerFragment.java @@ -463,12 +463,15 @@ public Object instantiateItem(ViewGroup container, final int position) { if (mObsJson != null) { try { int obsPhotoCount = mObservation.photos != null ? mObservation.photos.size() : 0; - JSONArray obsSounds = new JSONObject(mObsJson).getJSONArray("observation_sounds"); - - if ((position - obsPhotoCount < obsSounds.length()) && (position - obsPhotoCount >= 0)) { - BetterJSONObject obsSound = new BetterJSONObject(obsSounds.getJSONObject(position - obsPhotoCount)); - item = new BetterJSONObject(obsSound.getJSONObject("sound")); - isHidden = new ObservationSound(obsSound).hidden; + JSONObject json = new JSONObject(mObsJson); + if (json.has("observation_sounds")) { + JSONArray obsSounds = json.getJSONArray("observation_sounds"); + + if ((position - obsPhotoCount < obsSounds.length()) && (position - obsPhotoCount >= 0)) { + BetterJSONObject obsSound = new BetterJSONObject(obsSounds.getJSONObject(position - obsPhotoCount)); + item = new BetterJSONObject(obsSound.getJSONObject("sound")); + isHidden = new ObservationSound(obsSound).hidden; + } } } catch (JSONException e) { throw new RuntimeException(e); @@ -486,12 +489,15 @@ public Object instantiateItem(ViewGroup container, final int position) { if (mObsJson != null) { try { - JSONArray obsPhotos = new JSONObject(mObsJson).getJSONArray("observation_photos"); - - if (position < obsPhotos.length()) { - BetterJSONObject obsPhoto = new BetterJSONObject(obsPhotos.getJSONObject(position)); - item = new BetterJSONObject(obsPhoto.getJSONObject("photo")); - isHidden = new ObservationPhoto(obsPhoto).hidden; + JSONObject json = new JSONObject(mObsJson); + if (json.has("observation_photos")) { + JSONArray obsPhotos = json.getJSONArray("observation_photos"); + + if (position < obsPhotos.length()) { + BetterJSONObject obsPhoto = new BetterJSONObject(obsPhotos.getJSONObject(position)); + item = new BetterJSONObject(obsPhoto.getJSONObject("photo")); + isHidden = new ObservationPhoto(obsPhoto).hidden; + } } } catch (JSONException e) { Logger.error(e);