From c998ef1f560a2e92df45a1e6ea1f8e92179c26e9 Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 21 Jun 2022 16:01:27 +0200 Subject: [PATCH] Fix some crashes --- .../android/activities/ContextActivity.java | 39 ++++++++++--------- .../android/ui/drawer/StatusAdapter.java | 2 +- .../timeline/FragmentMastodonTimeline.java | 28 ++++++------- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/activities/ContextActivity.java b/app/src/main/java/app/fedilab/android/activities/ContextActivity.java index 809e9875..d8cf8cf2 100644 --- a/app/src/main/java/app/fedilab/android/activities/ContextActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/ContextActivity.java @@ -97,25 +97,26 @@ public class ContextActivity extends BaseActivity { }).start(); StatusesVM timelinesVM = new ViewModelProvider(ContextActivity.this).get(StatusesVM.class); timelinesVM.getStatus(MainActivity.currentInstance, MainActivity.currentToken, focusedStatus.id).observe(ContextActivity.this, status -> { - StatusCache statusCache = new StatusCache(); - statusCache.instance = MainActivity.currentInstance; - statusCache.user_id = MainActivity.currentUserID; - statusCache.status = status; - statusCache.status_id = status.id; - //Update cache - new Thread(() -> { - try { - new StatusCache(getApplication()).updateIfExists(statusCache); - new QuickLoad(getApplication().getApplicationContext()).updateStatus(Helper.getCurrentAccount(ContextActivity.this), status); - Handler mainHandler = new Handler(Looper.getMainLooper()); - //Update UI - Runnable myRunnable = () -> sendAction(ContextActivity.this, Helper.ARG_STATUS_ACTION, status, null); - mainHandler.post(myRunnable); - } catch (DBException e) { - e.printStackTrace(); - } - }).start(); - + if (status != null) { + StatusCache statusCache = new StatusCache(); + statusCache.instance = MainActivity.currentInstance; + statusCache.user_id = MainActivity.currentUserID; + statusCache.status = status; + statusCache.status_id = status.id; + //Update cache + new Thread(() -> { + try { + new StatusCache(getApplication()).updateIfExists(statusCache); + new QuickLoad(getApplication().getApplicationContext()).updateStatus(Helper.getCurrentAccount(ContextActivity.this), status); + Handler mainHandler = new Handler(Looper.getMainLooper()); + //Update UI + Runnable myRunnable = () -> sendAction(ContextActivity.this, Helper.ARG_STATUS_ACTION, status, null); + mainHandler.post(myRunnable); + } catch (DBException e) { + e.printStackTrace(); + } + }).start(); + } }); } diff --git a/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java b/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java index ca77e0b6..7ad368a5 100644 --- a/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java +++ b/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java @@ -1663,7 +1663,7 @@ public class StatusAdapter extends RecyclerView.Adapter int position = 0; if (statusList != null) { for (Status _status : statusList) { - if (_status.id.compareTo(status.id) == 0 || (_status.reblog != null && _status.reblog.id.compareTo(status.id) == 0)) { + if (status.id != null && ((_status.id != null && _status.id.compareTo(status.id) == 0) || (_status.reblog != null && _status.reblog.id != null && _status.reblog.id.compareTo(status.id) == 0))) { break; } position++; diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java index d04106a4..407c186b 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java @@ -512,20 +512,22 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. @Override public void onDestroyView() { //Update last read id for home timeline - if (mLayoutManager != null) { - int position = mLayoutManager.findFirstVisibleItemPosition(); - new Thread(() -> { - try { - new QuickLoad(requireActivity()).storeTimeline(position, user_id, instance, timelineType, statuses, ident); - } catch (Exception ignored) { - } - }).start(); + if (isAdded()) { + if (mLayoutManager != null) { + int position = mLayoutManager.findFirstVisibleItemPosition(); + new Thread(() -> { + try { + new QuickLoad(requireActivity()).storeTimeline(position, user_id, instance, timelineType, statuses, ident); + } catch (Exception ignored) { + } + }).start(); + } + storeMarker(); + if (binding != null) { + binding.recyclerView.setAdapter(null); + } + LocalBroadcastManager.getInstance(requireActivity()).unregisterReceiver(receive_action); } - storeMarker(); - if (binding != null) { - binding.recyclerView.setAdapter(null); - } - LocalBroadcastManager.getInstance(requireActivity()).unregisterReceiver(receive_action); statusAdapter = null; binding = null; super.onDestroyView();