Repository: thundernest/k-9
Issue: Crash when clicking message list item

### 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 10/12/13

### Device model

_No response_

### Steps to reproduce

I don't know.

Click an item shortly before it is removed from the list (click handler runs after the item has been removed from the list)?

### Expected behavior

Don't crash

### Actual behavior

Crash

### Logs

Stack trace via the Google Play developer console:

```text
Exception java.lang.IllegalStateException:
  at com.fsck.k9.ui.messagelist.MessageListAdapter.getItemFromView (MessageListAdapter.kt:521)
  at com.fsck.k9.ui.messagelist.MessageListAdapter.messageClickedListener$lambda$6 (MessageListAdapter.kt:165)
  at com.fsck.k9.ui.messagelist.MessageListAdapter.$r8$lambda$LKUPKU_VJZX9lrI-jWyV1hC0_JE
  at com.fsck.k9.ui.messagelist.MessageListAdapter$$ExternalSyntheticLambda1.onClick
  at android.view.View.performClick (View.java:7506)
  at android.view.View.performClickInternal (View.java:7483)
  at android.view.View.-$$Nest$mperformClickInternal
  at android.view.View$PerformClick.run (View.java:29335)
  at android.os.Handler.handleCallback (Handler.java:942)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loopOnce (Looper.java:201)
  at android.os.Looper.loop (Looper.java:288)
  at android.app.ActivityThread.main (ActivityThread.java:7898)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:548)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:936)
```

Changed files:
app/ui/legacy/src/main/java/com/fsck/k9/ui/messagelist/MessageListAdapter.kt

diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagelist/MessageListAdapter.kt b/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagelist/MessageListAdapter.kt
index 49988afb8..4072c6bd2 100644
--- a/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagelist/MessageListAdapter.kt
+++ b/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagelist/MessageListAdapter.kt
@@ -162,13 +162,14 @@ class MessageListAdapter internal constructor(
         }
 
     private val messageClickedListener = OnClickListener { view: View ->
-        val messageListItem = getItemFromView(view)
+        val messageListItem = getItemFromView(view) ?: return@OnClickListener
         listItemListener.onMessageClicked(messageListItem)
     }
 
     private val messageLongClickedListener = OnLongClickListener { view: View ->
-        val messageListItem = getItemFromView(view)
-        listItemListener.onToggleMessageSelection(messageListItem)
+        getItemFromView(view)?.let { messageListItem ->
+            listItemListener.onToggleMessageSelection(messageListItem)
+        }
         true
     }
 
@@ -177,13 +178,13 @@ class MessageListAdapter internal constructor(
     }
 
     private val flagClickListener = OnClickListener { view: View ->
-        val messageListItem = getItemFromView(view)
+        val messageListItem = getItemFromView(view) ?: return@OnClickListener
         listItemListener.onToggleMessageFlag(messageListItem)
     }
 
     private val contactPictureClickListener = OnClickListener { view: View ->
         val parentView = view.parent.parent as View
-        val messageListItem = getItemFromView(parentView)
+        val messageListItem = getItemFromView(parentView) ?: return@OnClickListener
         listItemListener.onToggleMessageSelection(messageListItem)
     }
 
@@ -516,9 +517,9 @@ class MessageListAdapter internal constructor(
             .sumOf { it.threadCount.coerceAtLeast(1) }
     }
 
-    private fun getItemFromView(view: View): MessageListItem {
+    private fun getItemFromView(view: View): MessageListItem? {
         val messageViewHolder = view.tag as MessageViewHolder
-        return getItemById(messageViewHolder.uniqueId) ?: error("Couldn't find MessageListItem by View")
+        return getItemById(messageViewHolder.uniqueId)
     }
 }