瀏覽代碼

try to avoid NPE for currentConversation

On gplay console the following NPE was reported for the line

participantPermissions = ParticipantPermissions(spreedCapabilities, currentConversation!!)

 E  FATAL EXCEPTION: main
          Process: com.nextcloud.talk2, PID: 6626
          java.lang.NullPointerException
          at com.nextcloud.talk.chat.ChatActivity.initObservers$lambda$13(ChatActivity.kt:583)
          at com.nextcloud.talk.chat.ChatActivity.$r8$lambda$QKH5JCFLmCzRMlSJ-EV-m4IW5ig(Unknown Source:0)

which seems that currentConversation was null. If it would have been spreedCapabilities, then the error would have been thrown in the line before..

A reason MAY BE that the observer is triggered before setData on the ViewModel is executed.
While this fix is just not executing code when currentConversation is null, it's unsure if it will follow up problems (like an empty chat) or if the observer is triggered another time when currentConversation is available.

So it's just a hotfix.
To improve the situation in the long term, we should move more logic to viewModel and only use Flow instead to mix it with LiveData.

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
Marcel Hibbe 8 月之前
父節點
當前提交
b78267d96f
共有 1 個文件被更改,包括 48 次插入33 次删除
  1. 48 33
      app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt

+ 48 - 33
app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt

@@ -571,48 +571,63 @@ class ChatActivity :
         chatViewModel.getCapabilitiesViewState.observe(this) { state ->
             when (state) {
                 is ChatViewModel.GetCapabilitiesUpdateState -> {
-                    spreedCapabilities = state.spreedCapabilities
-                    chatApiVersion = ApiUtils.getChatApiVersion(spreedCapabilities, intArrayOf(1))
-                    participantPermissions = ParticipantPermissions(spreedCapabilities, currentConversation!!)
-
-                    invalidateOptionsMenu()
-                    checkShowCallButtons()
-                    checkLobbyState()
-                    updateRoomTimerHandler()
+                    if (currentConversation != null) {
+                        spreedCapabilities = state.spreedCapabilities
+                        chatApiVersion = ApiUtils.getChatApiVersion(spreedCapabilities, intArrayOf(1))
+                        participantPermissions = ParticipantPermissions(spreedCapabilities, currentConversation!!)
+
+                        invalidateOptionsMenu()
+                        checkShowCallButtons()
+                        checkLobbyState()
+                        updateRoomTimerHandler()
+                    } else {
+                        Log.w(
+                            TAG,
+                            "currentConversation was null in observer ChatViewModel.GetCapabilitiesUpdateState"
+                        )
+                    }
                 }
 
                 is ChatViewModel.GetCapabilitiesInitialLoadState -> {
-                    spreedCapabilities = state.spreedCapabilities
-                    chatApiVersion = ApiUtils.getChatApiVersion(spreedCapabilities, intArrayOf(1))
-                    participantPermissions = ParticipantPermissions(spreedCapabilities, currentConversation!!)
-
-                    supportFragmentManager.commit {
-                        setReorderingAllowed(true) // optimizes out redundant replace operations
-                        replace(R.id.fragment_container_activity_chat, messageInputFragment)
-                    }
+                    if (currentConversation != null) {
+                        spreedCapabilities = state.spreedCapabilities
+                        chatApiVersion = ApiUtils.getChatApiVersion(spreedCapabilities, intArrayOf(1))
+                        participantPermissions = ParticipantPermissions(spreedCapabilities, currentConversation!!)
+
+                        supportFragmentManager.commit {
+                            setReorderingAllowed(true) // optimizes out redundant replace operations
+                            replace(R.id.fragment_container_activity_chat, messageInputFragment)
+                        }
 
-                    joinRoomWithPassword()
+                        joinRoomWithPassword()
 
-                    if (conversationUser?.userId != "?" &&
-                        CapabilitiesUtil.hasSpreedFeatureCapability(spreedCapabilities, SpreedFeatures.MENTION_FLAG)
-                    ) {
-                        binding.chatToolbar.setOnClickListener { _ -> showConversationInfoScreen() }
-                    }
+                        if (conversationUser?.userId != "?" &&
+                            CapabilitiesUtil.hasSpreedFeatureCapability(spreedCapabilities, SpreedFeatures.MENTION_FLAG)
+                        ) {
+                            binding.chatToolbar.setOnClickListener { _ -> showConversationInfoScreen() }
+                        }
 
-                    loadAvatarForStatusBar()
-                    setupSwipeToReply()
-                    setActionBarTitle()
+                        loadAvatarForStatusBar()
+                        setupSwipeToReply()
+                        setActionBarTitle()
 
-                    checkShowCallButtons()
-                    checkLobbyState()
-                    updateRoomTimerHandler()
+                        checkShowCallButtons()
+                        checkLobbyState()
+                        updateRoomTimerHandler()
 
-                    val urlForChatting = ApiUtils.getUrlForChat(chatApiVersion, conversationUser?.baseUrl, roomToken)
+                        val urlForChatting =
+                            ApiUtils.getUrlForChat(chatApiVersion, conversationUser?.baseUrl, roomToken)
 
-                    if (adapter?.isEmpty == true) {
-                        chatViewModel.loadMessages(
-                            withCredentials = credentials!!,
-                            withUrl = urlForChatting
+                        if (adapter?.isEmpty == true) {
+                            chatViewModel.loadMessages(
+                                withCredentials = credentials!!,
+                                withUrl = urlForChatting
+                            )
+                        }
+                    } else {
+                        Log.w(
+                            TAG,
+                            "currentConversation was null in observer ChatViewModel.GetCapabilitiesInitialLoadState"
                         )
                     }
                 }