在 Twilio Chat SDK 的私人频道中添加成员时应用程序崩溃

App Crashing while adding a member in an Private channel in Twilio Chat SDK

我正在实施 Twilio Chat SDK,我会在私人频道中添加成员。流程是这样的,首先我检查我的列表中是否有频道。如果它存在,那么我加入它。如果我的列表中没有频道,我首先创建它,然后加入它,然后使用它的唯一名称添加成员。当我 运行 我的应用程序创建客户端时,在通道进程完成后它崩溃并出现以下错误:

com.twilio.chat.ListenerException: Exception thrown by a listener. Your application might have a problem in listener implementation. Listeners must never throw uncaught exceptions. See 'Caused by:' below for more details.
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at com.twilio.chat.internal.RethrowingForwarder$RethrowingProxy.invoke(RethrowingForwarder.java:123)
        at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
        at $Proxy17.onError(Unknown Source)
        at com.twilio.chat.internal.StatusListenerForwarder.run(StatusListenerForwarder.java:46)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:8167)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.twilio.chat.Members.addByIdentity(java.lang.String, com.twilio.chat.StatusListener)' on a null object reference
        at com.zofeur.customer.views.fragments.SupportFragment.addMemberInChannel(SupportFragment.kt:259)
        at com.zofeur.customer.views.fragments.SupportFragment.access$addMemberInChannel(SupportFragment.kt:22)
        at com.zofeur.customer.views.fragments.SupportFragment$joinChannel.onError(SupportFragment.kt:249)

我一直在寻找多种解决方案,但无法找到。

下面是我的片段的代码:

支持片段:

private fun createChannel(channelUniqueName: String) {
    mViewModel.chatClient?.channels?.getChannel(channelUniqueName,
        object : CallbackListener<Channel>() {
            override fun onSuccess(channel: Channel?) {
                if (channel != null) {
                    mViewModel.channel = channel
                    joinChannel(channel)

                }
            }

            override fun onError(errorInfo: ErrorInfo?) {
                super.onError(errorInfo)
                if (errorInfo?.code == Constants.ERROR_CHANNEL_NOT_FOUND) {
                    mViewModel.chatClient?.channels?.channelBuilder()
                        ?.withUniqueName(channelUniqueName)
                        ?.withType(Channel.ChannelType.PRIVATE)
                        ?.build(object : CallbackListener<Channel>() {
                            override fun onSuccess(channel: Channel?) {
                                requireContext().T("Channel Created $channel")
                                if (channel != null) {
                                    mViewModel.channel = channel
                                    joinChannel(channel)
                                }
                            }

                        })
                } else {
                    requireContext().T("Channel not created" + errorInfo.toString())

                }
            }
        })

}

private fun joinChannel(channel: Channel) {
    channel.join(object : StatusListener() {
        override fun onSuccess() {
            requireContext().T("Channel Joined" + channel.uniqueName)
            addMemberInChannel(channel, identity)
        }

        override fun onError(errorInfo: ErrorInfo) {
            if (errorInfo.code == Constants.ERROR_MEMBER_ALREADY_EXISTS) {
                // already join member
                addMemberInChannel(channel, identity)
            } else {
                requireContext().T("Error joining channel$errorInfo")
            }
        }
    })
}

private fun addMemberInChannel(channel: Channel, identity: String) {
    if (mViewModel.channel.createdBy == mViewModel.chatClient.myIdentity) {
        channel.members.addByIdentity(identity, object : StatusListener() {
            override fun onSuccess() {
                requireContext().T("Member added successfully" + channel.uniqueName)
            }

            override fun onError(errorInfo: ErrorInfo?) {
                super.onError(errorInfo)
                requireContext().T("Channel member added error" + errorInfo.toString())
            }
        })

    } else {
        requireContext().T("You cannot add member, You don't have rights.")
    }
}

任何形式的帮助都会很有帮助。提前致谢。

嗨,我遇到了同样的问题当我调试更长时间时,我发现当你创建一个频道时它有 3 个状态:

  1. SynchronizationStatus_STARTED
  2. SynchronizationStatus_CHANNELS_COMPLETED
  3. SyncronizationStatus_COMPLETED

您需要在客户端之后执行任何与频道相关的操作SyncronizationStatus_COMPLETED希望它能正常工作谢谢就这样

client.addListener(object :ChatClientListener{
    override fun onClientSynchronization(p0: 
   ChatClient.SynchronizationStatus?) {
                                if (p0 == ChatClient.SynchronizationStatus.COMPLETED) {
                                    // Client is now ready for business, start working
                                    mViewModel.chatClient = client
                                   
                                }
                            }
    }