gpt4 book ai didi

android - 快速 fragment 切换 View 绑定(bind) null

转载 作者:行者123 更新时间:2023-12-04 23:38:36 26 4
gpt4 key购买 nike

我在选项卡布局中有 2 个 fragment ,它们之间的快速切换会导致 View 返回为空,使用 ViewBinding。这是因为构建 FragmentXBinding 类的延迟吗?
用法示例:

chatadapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
binding.chatRecyclerView.smoothScrollToPosition(0);
}
});
错误:
java.lang.NullPointerException: Attempt to read from field 'androidx.recyclerview.widget.RecyclerView com.iku.databinding.FragmentChatBinding.chatRecyclerView' on a null object reference

最佳答案

fragment X

 FragmentXBinding binding;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentChatBinding.inflate(inflater, container, false);
init();
return binding.getRoot();
}

private void init() {
// all initialization
}
这有助于我用空引用解决我的错误

关于android - 快速 fragment 切换 View 绑定(bind) null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63701417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com