Repository: thundernest/k-9
Issue: Crash when swiping to next/previous message

### Checklist

- [X] I have used the search function to see if someone else has already submitted the same bug report.
- [X] I will describe the problem with as much detail as possible.

### App version

6.309

### Where did you get the app from?

Google Play

### Android version

Android 11/12

### Device model

_No response_

### Steps to reproduce

I don't know

### Expected behavior

Don't crash

### Actual behavior

Crash

### Logs

Strack trace (via Google Play developer console):

```text
Exception java.lang.IllegalStateException: Check failed.
  at com.fsck.k9.ui.messageview.MessageViewContainerFragment$MessageViewContainerAdapter.getMessageReference (MessageViewContainerFragment.kt:269)
  at com.fsck.k9.ui.messageview.MessageViewContainerFragment.setActiveMessage (MessageViewContainerFragment.kt:150)
  at com.fsck.k9.ui.messageview.MessageViewContainerFragment.access$setActiveMessage (MessageViewContainerFragment.kt:25)
  at com.fsck.k9.ui.messageview.MessageViewContainerFragment$onCreateView$1.onPageScrollStateChanged (MessageViewContainerFragment.kt:103)
  at androidx.viewpager2.widget.CompositeOnPageChangeCallback.onPageScrollStateChanged (CompositeOnPageChangeCallback.java:87)
  at androidx.viewpager2.widget.CompositeOnPageChangeCallback.onPageScrollStateChanged (CompositeOnPageChangeCallback.java:87)
  at androidx.viewpager2.widget.ScrollEventAdapter.dispatchStateChanged (ScrollEventAdapter.java:426)
  at androidx.viewpager2.widget.ScrollEventAdapter.onScrollStateChanged (ScrollEventAdapter.java:152)
  at androidx.recyclerview.widget.RecyclerView.dispatchOnScrollStateChanged (RecyclerView.java:5384)
  at androidx.recyclerview.widget.RecyclerView.setScrollState (RecyclerView.java:1618)
  at androidx.recyclerview.widget.RecyclerView.onTouchEvent (RecyclerView.java:3558)
  at androidx.viewpager2.widget.ViewPager2$RecyclerViewImpl.onTouchEvent (ViewPager2.java:991)
  at android.view.View.dispatchTouchEvent (View.java:14900)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3178)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2820)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3184)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2834)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3184)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2834)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3184)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2834)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3184)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2834)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3184)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2834)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3184)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2834)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3184)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2834)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3184)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2834)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3184)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2834)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3184)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2834)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3184)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2834)
  at com.android.internal.policy.DecorView.superDispatchTouchEvent (DecorView.java:570)
  at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent (PhoneWindow.java:1895)
  at android.app.Activity.dispatchTouchEvent (Activity.java:4309)
  at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent (WindowCallbackWrapper.java:70)
  at com.android.internal.policy.DecorView.dispatchTouchEvent (DecorView.java:528)
  at android.view.View.dispatchPointerEvent (View.java:15169)
  at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent (ViewRootImpl.java:6872)
  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess (ViewRootImpl.java:6658)
  at android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:6097)
  at android.view.ViewRootImpl$InputStage.onDeliverToNext (ViewRootImpl.java:6154)
  at android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:6120)
  at android.view.ViewRootImpl$AsyncInputStage.forward (ViewRootImpl.java:6285)
  at android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:6128)
  at android.view.ViewRootImpl$AsyncInputStage.apply (ViewRootImpl.java:6342)
  at android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:6101)
  at android.view.ViewRootImpl$InputStage.onDeliverToNext (ViewRootImpl.java:6154)
  at android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:6120)
  at android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:6128)
  at android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:6101)
  at android.view.ViewRootImpl.deliverInputEvent (ViewRootImpl.java:9208)
  at android.view.ViewRootImpl.doProcessInputEvents (ViewRootImpl.java:9148)
  at android.view.ViewRootImpl.enqueueInputEvent (ViewRootImpl.java:9105)
  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent (ViewRootImpl.java:9350)
  at android.view.InputEventReceiver.dispatchInputEvent (InputEventReceiver.java:280)
  at android.os.MessageQueue.nativePollOnce
  at android.os.MessageQueue.next (MessageQueue.java:337)
  at android.os.Looper.loopOnce (Looper.java:168)
  at android.os.Looper.loop (Looper.java:299)
  at android.app.ActivityThread.main (ActivityThread.java:8306)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:576)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1073)
```

Changed files:
app/ui/legacy/src/main/java/com/fsck/k9/ui/messageview/MessageViewContainerFragment.kt

diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/ui/messageview/MessageViewContainerFragment.kt b/app/ui/legacy/src/main/java/com/fsck/k9/ui/messageview/MessageViewContainerFragment.kt
index f5ffabb4d..d5dd3509f 100644
--- a/app/ui/legacy/src/main/java/com/fsck/k9/ui/messageview/MessageViewContainerFragment.kt
+++ b/app/ui/legacy/src/main/java/com/fsck/k9/ui/messageview/MessageViewContainerFragment.kt
@@ -145,20 +145,19 @@ class MessageViewContainerFragment : Fragment() {
     }
 
     private fun setActiveMessage(position: Int) {
-        // If the position of current message changes (e.g. because messages were added or removed from the list), we
-        // keep track of the new position but otherwise ignore the event.
-        val newMessageReference = adapter.getMessageReference(position)
+        val newMessageReference = adapter.getMessageReference(position) ?: return
         if (newMessageReference == activeMessageReference) {
+            // If the position of current message changes (e.g. because messages were added or removed from the list),
+            // we keep track of the new position but otherwise ignore the event.
             currentPosition = position
             return
         }
 
         rememberNavigationDirection(position)
 
-        messageReference = adapter.getMessageReference(position)
-
-        activeMessageReference = messageReference
-        fragmentListener.setActiveMessage(messageReference)
+        messageReference = newMessageReference
+        activeMessageReference = newMessageReference
+        fragmentListener.setActiveMessage(newMessageReference)
     }
 
     private fun rememberNavigationDirection(newPosition: Int) {
@@ -261,10 +260,8 @@ class MessageViewContainerFragment : Fragment() {
             return MessageViewFragment.newInstance(messageReference)
         }
 
-        fun getMessageReference(position: Int): MessageReference {
-            check(position in messageList.indices)
-
-            return messageList[position].messageReference
+        fun getMessageReference(position: Int): MessageReference? {
+            return messageList.getOrNull(position)?.messageReference
         }
 
         fun getPosition(messageReference: MessageReference): Int {