Repository: thundernest/k-9
Issue: Crash when trying to delete a message from message view before it was loaded

### 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.302

### Where did you get the app from?

_No response_

### Android version

doesn't matter

### Device model

_No response_

### Steps to reproduce

1. Open a message for viewing
2. Press delete button in toolbar before loading the message has completed.
3. Crash

This is easier to reproduce when manually adding a `sleep()` call to `LocalMessageLoader.loadInBackground()`.

### Expected behavior

Ideally, we disable or hide toolbar buttons when the message hasn't been loaded yet. But this might lead to flickering because usually loading the message is fast, but not "users won't see the change" fast.
At the very least we should ignore actions operating on the message until it has been loaded.

### Actual behavior

Crash

### Logs

```text
Exception java.lang.IllegalStateException: Required value was null.
  at com.fsck.k9.ui.messageview.MessageViewFragment.onDelete (MessageViewFragment.kt:383)
  at com.fsck.k9.ui.messageview.MessageViewFragment.onOptionsItemSelected (MessageViewFragment.kt:283)
  at androidx.fragment.app.Fragment.performOptionsItemSelected (Fragment.java:3263)
  at androidx.fragment.app.FragmentManager.dispatchOptionsItemSelected (FragmentManager.java:3065)
  at androidx.fragment.app.Fragment.performOptionsItemSelected (Fragment.java:3267)
  at androidx.fragment.app.FragmentManager.dispatchOptionsItemSelected (FragmentManager.java:3065)
  at androidx.fragment.app.FragmentManager$2.onMenuItemSelected (FragmentManager.java:476)
  at androidx.core.view.MenuHostHelper.onMenuItemSelected (MenuHostHelper.java:90)
  at androidx.activity.ComponentActivity.onMenuItemSelected (ComponentActivity.java:518)
  at androidx.fragment.app.FragmentActivity.onMenuItemSelected (FragmentActivity.java:265)
  at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected (AppCompatActivity.java:264)
  at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected (WindowCallbackWrapper.java:109)
  at androidx.appcompat.app.ToolbarActionBar$2.onMenuItemClick (ToolbarActionBar.java:66)
  at androidx.appcompat.widget.Toolbar$1.onMenuItemClick (Toolbar.java:221)
  at androidx.appcompat.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected (ActionMenuView.java:781)
  at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected (MenuBuilder.java:834)
  at androidx.appcompat.view.menu.MenuItemImpl.invoke (MenuItemImpl.java:158)
  at androidx.appcompat.view.menu.MenuBuilder.performItemAction (MenuBuilder.java:985)
  at androidx.appcompat.view.menu.MenuBuilder.performItemAction (MenuBuilder.java:975)
  at androidx.appcompat.widget.ActionMenuView.invokeItem (ActionMenuView.java:625)
  at androidx.appcompat.view.menu.ActionMenuItemView.onClick (ActionMenuItemView.java:151)
  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/messageview/MessageViewFragment.kt

diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.kt b/app/ui/legacy/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.kt
index 6cf284556..a7de139a0 100644
--- a/app/ui/legacy/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.kt
+++ b/app/ui/legacy/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.kt
@@ -278,6 +278,8 @@ class MessageViewFragment :
     }
 
     override fun onOptionsItemSelected(item: MenuItem): Boolean {
+        if (message == null) return false
+
         when (item.itemId) {
             R.id.toggle_message_view_theme -> onToggleTheme()
             R.id.delete -> onDelete()