TencentCloud / TIMSDK

Tencent Cloud Chat features a comprehensive suite of solutions including global access, one-to-one chat, group chat, message push, profile and relationship chain hosting, and account authentication.
https://trtc.io/products/chat
2.54k stars 2.77k forks source link

【SDk 6.6.3002 - 单聊部分】ChatPresenter.setMessageListAdapter()' on a null object reference #1254

Closed captain-miao closed 1 year ago

captain-miao commented 1 year ago

如果你发布的应用出现这种闪退日志参考尾部修复方案。这个闪退是因为 Activity 销毁重建的时候,Fragment 的恢复只恢复 setArguments() 的参数,而且 Fragment 需要一个空参数的构造函数。参考 -> 为什么使用Fragment时必须提供一个无参的构造函数?

issue标题:ChatPresenter.setMessageListAdapter()' on a null object reference

【Android】-【SDk 6.6.3002 @2022.08.18 - Enhanced Edition - 增强版 - 单聊部分】 issue内容:

[ java.lang.NullPointerException]
Attempt to invoke virtual method 'void com.tencent.qcloud.tuikit.tuichat.presenter.ChatPresenter.setMessageListAdapter(com.tencent.qcloud.tuikit.tuichat.ui.interfaces.IMessageAdapter)' on a null object reference
com.tencent.qcloud.tuikit.tuichat.ui.view.ChatView.setPresenter(ChatView.java:297)

【当前现象】:闪退 【集成包】:IMSDK 【机型】: 【之前版本有无此问题】: 【有无日志或者描述、截图】: -------------------------完整的示例如下:-------------------------

java.lang.NullPointerException

Attempt to invoke virtual method 'void com.tencent.qcloud.tuikit.tuichat.presenter.ChatPresenter.setMessageListAdapter(com.tencent.qcloud.tuikit.tuichat.ui.interfaces.IMessageAdapter)' on a null object reference

com.tencent.qcloud.tuikit.tuichat.ui.view.ChatView.setPresenter(ChatView.java:297)

com.tencent.qcloud.tuikit.tuichat.ui.page.TUIC2CChatFragment.initView(TUIC2CChatFragment.java:55)

------------------------- 修复方案 ------------------------- 将Activity中的 C2CChatPresenter 成员移到 TUIC2CChatFragment 中。群聊也一样处理。

// https://github.com/TencentCloud/TIMSDK/blob/master/Android/TUIKit/TUIChat/tuichat/src/main/java/com/tencent/qcloud/tuikit/tuichat/ui/page/TUIC2CChatActivity.java
// 删除  C2CChatPresenter presenter
public class TUIC2CChatActivity extends TUIBaseChatActivity {
    private static final String TAG = TUIC2CChatActivity.class.getSimpleName();

    private TUIC2CChatFragment chatFragment;
    //private C2CChatPresenter presenter;
    @Override
    public void initChat(ChatInfo chatInfo) {
        TUIChatLog.i(TAG, "inti chat " + chatInfo);

        if (!TUIChatUtils.isC2CChat(chatInfo.getType())) {
            TUIChatLog.e(TAG, "init C2C chat failed , chatInfo = " + chatInfo);
            ToastUtil.toastShortMessage("init c2c chat failed.");
        }
        chatFragment = new TUIC2CChatFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(TUIChatConstants.CHAT_INFO, chatInfo);
        chatFragment.setArguments(bundle);
        //presenter = new C2CChatPresenter();
        //presenter.initListener();
        //chatFragment.setPresenter(presenter);
        getSupportFragmentManager().beginTransaction().replace(R.id.empty_view, chatFragment).commitAllowingStateLoss();
    }
}

// 在 Fragment 创建和初始化 presenter
public class TUIC2CChatFragment extends TUIBaseChatFragment {
    private static final String TAG = TUIC2CChatFragment.class.getSimpleName();

    private ChatInfo chatInfo;
    private C2CChatPresenter presenter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        TUIChatLog.i(TAG, "oncreate view " + this);

        baseView = super.onCreateView(inflater, container, savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle == null) {
            return baseView;
        }
        chatInfo = (ChatInfo) bundle.getSerializable(TUIChatConstants.CHAT_INFO);
        if (chatInfo == null) {
            return baseView;
        }
        // 创建和初始化 C2CChatPresenter
        presenter = new C2CChatPresenter();
        presenter.initListener();

        initView();

        return baseView;
    }

    @Override
    protected void initView() {
        super.initView();

        titleBar.setOnRightClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString(TUIConstants.TUIChat.CHAT_ID, chatInfo.getId());
                bundle.putString(TUIConstants.TUIChat.CHAT_BACKGROUND_URI, mChatBackgroundThumbnailUrl);
                TUICore.startActivity("FriendProfileActivity", bundle);
            }
        });

        chatView.setPresenter(presenter);
        presenter.setChatInfo(chatInfo);
        presenter.setTypingListener(chatView.mTypingListener);
        chatView.setChatInfo(chatInfo);
    }

//    public void setPresenter(C2CChatPresenter presenter) {
//        this.presenter = presenter;
//    }

    @Override
    public C2CChatPresenter getPresenter() {
        return presenter;
    }

    @Override
    public ChatInfo getChatInfo() {
        return chatInfo;
    }
}